99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python基礎(chǔ)教程之元組操作使用詳解
python基礎(chǔ)教程之元組操作使用詳解
2017-12-24
收藏

python基礎(chǔ)教程之元組操作使用詳解

這篇文章主要介紹了python的元組操作使用方法,需要的朋友可以參考下


簡介

tuple

1.元組是以圓括號“()”包圍的數(shù)據(jù)集合,不同成員以“,”分隔。通過下標進行訪問

2.不可變序列,可以看做不可變的列表,與列表不同:元組中數(shù)據(jù)一旦確立就不能改變(所以沒有類似列表的增刪改操作,只有基本序列操作)

3.支持任意類型,任意嵌套以及常見的序列操作

4.元組通常用在使語句或用戶定義的函數(shù)能夠安全地采用一組值的時候,即被使用的元組的值不會改變

聲明及使用

代碼如下:
t = ()  #空元組
t =(1,)  #單個元素元組,注意逗號必須
t =(1,2,3)


1 in t #判斷
2 not in t

#其他同序列基本操作:分片,索引
print t[0]
print t[-1]
print t[:2]

#不會對原來元組造成影響
print t+(4,5)  #返回新元組(1,2,3,4,5)
print t * 2    #(1,2,3,1,2,3)
t.index(1)
t.count(1)

#列表元組轉(zhuǎn)換
l = [1,2,3]
lt = tuple(l)
tl = list(lt)
lt_sorted = sorted(l)  #對元組進行排序,返回是列表

#字符串轉(zhuǎn)元組(得到字符元組序列)
print tuple('hello)   #('h','e','l','l','o')
tuple沒有append/extend/remove/pop等增刪改操作tuple沒有find

查看幫助

代碼如下:
help(tuple)

用途

1.賦值

代碼如下:
t = 1,2,3   #等價 t = (1, 2, 3)
x, y, z = t   #序列拆封,要求左側(cè)變量數(shù)目和右側(cè)序列長度相等

2.函數(shù)多個返回值

代碼如下:
def test():
    return (1,2)
x, y = test()

3.傳參[強制不改變原始序列]

代碼如下:
def print_list(l):
    t = tuple(l)   #或者t = l[:]
    dosomething()

4.字符串格式化

代碼如下:
print '%s is %s years old' % ('tom', 20)

5.作為字典的key

優(yōu)點
1.性能

tuple比列表操作速度快

若需要定義一個常量集,或者是只讀序列,唯一的操作是不斷遍歷之,使用tuple代替list

代碼如下:
>>> a = tuple(range(1000))
>>> b = range(1000)
>>> def test_t():
...     for i in a:
...             pass
...
>>> def test_l():
...     for i in b:
...             pass
...
>>> from timeit import Timer
>>> at = Timer("test_t()", "from __main__ import test_t")
>>> bt = Timer("test_l()", "from __main__ import test_l")

簡單測試

代碼如下:
>>> at.repeat(3, 100000)
[1.526214838027954, 1.5191287994384766, 1.5181210041046143]

>>> bt.repeat(3, 100000)
[1.5545141696929932, 1.557785987854004, 1.5511009693145752]

2.不可變性

對不需要的數(shù)據(jù)進行“寫保護”,使代碼更加安全

不可變性,若在程序中以列表形式傳遞對象集合,可能在任何地方被改變,使用元組,則不能

不可變性只適用于元組本身頂層而非其內(nèi)容,例如元組內(nèi)部的列表可以修改

代碼如下:
l = [1,2,3]
t = (1,2,l)
l.append(4)

不可變性提供了某種完整性,規(guī)范化,確保不會被修改,保持某種固定關(guān)系

修改的方法

代碼如下:

tuple -> list -> tuple



數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }