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

熱線電話:13121318867

登錄
首頁精彩閱讀python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
2018-02-04
收藏

python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解

python數(shù)據(jù)結(jié)構(gòu)之 列表和元組

序列:序列是一種數(shù)據(jù)結(jié)構(gòu),它包含的元素都進行了編號(從0開始)。典型的序列包括列表、字符串和元組。其中,列表是可變的(可以進行修改),而元組和字符串是不可變的(一旦創(chuàng)建了就是固定的)。序列中包含6種內(nèi)建的序列,包括列表、元組、字符串、Unicode字符串、buffer對象、xrange對象。
列表的聲明:    
mylist = []
2.列表的操作:
(1) 序列的分片:
 用法:mylist[startIndex:endIndex:step]
 exam:
  mylist[2:10] 檢索第2個字符到第10個字符,默認(rèn)步長為1.
  mylist[2:10:2] 檢索第2個字符到第10個字符,指定步長為2.
  mylist[-2:-1:2] 正數(shù)索引是相對于首部的坐標(biāo),負(fù)數(shù)是相對于尾部的坐標(biāo)。其實坐標(biāo)一定要小于終止坐標(biāo),否則返回空的分片。
  mylist[-12:-2:-2] 步長也可以是負(fù)數(shù),表示從右向左提取元素。
 
(2) 序列的索引
 用法:mylist[index]
 exam:
  mylist[2] mylist[-2]
  ps:正數(shù)是相對于首部的坐標(biāo),負(fù)數(shù)是相對于尾部的坐標(biāo)。
 
(3) 序列相加:
 用法: mylist1 + mylist2  <==>  [1,2] + [3,4]
 
 
(4) 序列乘法:
 用法: mylist * 5  mylist元素重復(fù)5次。
 
(5) in操作符:
 用法: ‘item' in mylist 判斷mylist是否包含某一成員。

3.列表涉及的內(nèi)建函數(shù): 內(nèi)建函數(shù)len、min、max針對列表操作非常有用。
    
(1) len函數(shù)返回序列中所包含元素的數(shù)量。
(2) min函數(shù)和max函數(shù)分別返回學(xué)列中最大和最小元素。
(3) list函數(shù)可以把字符串轉(zhuǎn)換成列表。
  exam: list('hello') => ['H','e','l','l','o']
(4) cmp函數(shù)用來比較2個元素的大小
  exam: cmp(x,y) => 返回0表示相等, -1 則是 x < y 1 則是 x > y
(5) reversed函數(shù)對序列進行反向迭代。
(6) sorted 返回已排序的包含seq所有元素的列表。

4.列表的方法:
    
(1)append: append方法在列表末尾追加新的對象。
  exam:lst=[1,2,3] lst.append(4) => [1,2,3,4]
(2)count: count方法統(tǒng)計某個元素在列表中出現(xiàn)的次數(shù)。
  exam: x=[[1,2],1,1,[2,1,[1,2]]] x.count(1) => 1
(3)extend: extend方法可以在列表的末尾一次性追加另外一個序列的多個值。即:可以用新列表擴展原有列表。
  exam: a=[1,2,3] b=[4,5,6] a.extend(b) => [1,2,3,4,5,6]
(4)index: index方法用于從列表中找出某個值第一個匹配項的索引位置。
  exam:lst=['we','le','at'] lst.index('le') => 1
(5)insert:insert方法用于將對象插入到列表中:
  exam:lst=[1,2,3,4,5,6] lst.insert(3,8) => [1,2,3,8,4,5,6]
(6)pop: pop方法移除列表中的一個元素(默認(rèn)是最后一個),并且返回該元素。
(7)remove: remove方法移除列表中某個值的第一個匹配項:
  exam: x=['to','be','or'] x.remove('to') => 你懂得。
(8)reverse 方法將列表中的元素反序。
(9)sort 方法用于在原位置對列表進行排序?!?br />   exam: sort方法有默認(rèn)的排序方法,另外還具有高級排序的用法,sort方法有兩個可選的參數(shù),key 和 reverse,key指定排序的關(guān)鍵字參數(shù),指定后排序會按key的大小來排序,reverse用于指定是否反序。
    x.sort(key=len) => 表示按照字符串的長度排序。
    x.sort(reverse=True) => 表示反序排序。
    x.sort(cmp) => 指定排序函數(shù),你懂的。

5.元組:元組和列表一樣,也是一種序列。唯一的不同是元組不能修改。
    
(1) 聲明方式:
  用逗號分隔一些值,就自動創(chuàng)建了元組。 exam: 1,2,3 => (1,2,3)
  也可以通過園括號聲明。 exam: (1,2,3) => (1,2,3)
 
(2) 元組的乘法:
  3*(40+2) => (42,42,42)

6.元組涉及的內(nèi)建函數(shù):    
(1)tuple函數(shù)的功能和list函數(shù)的基本上是一樣的:以一個序列作為參數(shù)并把它轉(zhuǎn)換為元組。
  exam: tuple([1,2,3]) => tuple(1,2,3)
7.元組的分片:    
exam: x=1,2,3 x[1] => 2 x[0:2] => (1,2)

數(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); }