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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀python中使用序列的方法
python中使用序列的方法
2017-08-09
收藏

python中使用序列的方法

本文實(shí)例講述了python中使用序列的方法。分享給大家供大家參考。具體如下:

列表、元組和字符串都是序列,但是序列是什么,它們?yōu)槭裁慈绱颂貏e呢?序列的兩個(gè)主要特點(diǎn)是索引操作符和切片操作符。索引操作符讓我們可以從序列中抓取一個(gè)特定項(xiàng)目。切片操作符讓我們能夠獲取序列的一個(gè)切片,即一部分序列。

#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]

輸出:    
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo

characters start to end is swaroop


它如何工作:

首先,我們來(lái)學(xué)習(xí)如何使用索引來(lái)取得序列中的單個(gè)項(xiàng)目。這也被稱作是下標(biāo)操作。每當(dāng)你用方括號(hào)中的一個(gè)數(shù)來(lái)指定一個(gè)序列的時(shí)候,Python會(huì)為你抓取序列中對(duì)應(yīng)位置的項(xiàng)目。記住,Python從0開(kāi)始計(jì)數(shù)。因此,shoplist[0]抓取第一個(gè)項(xiàng)目,shoplist[3]抓取shoplist序列中的第四個(gè)元素。

索引同樣可以是負(fù)數(shù),在那樣的情況下,位置是從序列尾開(kāi)始計(jì)算的。因此,shoplist[-1]表示序列的最后一個(gè)元素而shoplist[-2]抓取序列的倒數(shù)第二個(gè)項(xiàng)目。

切片操作符是序列名后跟一個(gè)方括號(hào),方括號(hào)中有一對(duì)可選的數(shù)字,并用冒號(hào)分割。注意這與你使用的索引操作符十分相似。記住數(shù)是可選的,而冒號(hào)是必須的。

切片操作符中的第一個(gè)數(shù)(冒號(hào)之前)表示切片開(kāi)始的位置,第二個(gè)數(shù)(冒號(hào)之后)表示切片到哪里結(jié)束。如果不指定第一個(gè)數(shù),Python就從序列首開(kāi)始。如果沒(méi)有指定第二個(gè)數(shù),則Python會(huì)停止在序列尾。注意,返回的序列從開(kāi)始位置 開(kāi)始 ,剛好在 結(jié)束 位置之前結(jié)束。即開(kāi)始位置是包含在序列切片中的,而結(jié)束位置被排斥在切片外。

這樣,shoplist[1:3]返回從位置1開(kāi)始,包括位置2,但是停止在位置3的一個(gè)序列切片,因此返回一個(gè)含有兩個(gè)項(xiàng)目的切片。類似地,shoplist[:]返回整個(gè)序列的拷貝。

你可以用負(fù)數(shù)做切片。負(fù)數(shù)用在從序列尾開(kāi)始計(jì)算的位置。例如,shoplist[:-1]會(huì)返回除了最后一個(gè)項(xiàng)目外包含所有項(xiàng)目的序列切片。

使用Python解釋器交互地嘗試不同切片指定組合,即在提示符下你能夠馬上看到結(jié)果。序列的神奇之處在于你可以用相同的方法訪問(wèn)元組、列表和字符串。



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

若不方便掃碼,搜微信號(hào):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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }