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

熱線電話:13121318867

登錄
首頁精彩閱讀Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典
Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典
2017-08-23
收藏

Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典

Python中有3種內(nèi)建的數(shù)據(jù)結(jié)構(gòu):列表、元組和字典。參考簡(jiǎn)明Python教程

1. 列表

list是處理一組有序項(xiàng)目的數(shù)據(jù)結(jié)構(gòu),即你可以在一個(gè)列表中存儲(chǔ)一個(gè) 序列 的項(xiàng)目。假想你有一個(gè)購(gòu)物列表,上面記載著你要買的東西,你就容易理解列表了。只不過在你的購(gòu)物表上,可能每樣?xùn)|西都獨(dú)自占有一行,而在Python中,你在每個(gè)項(xiàng)目之間用逗號(hào)分割。

列表中的項(xiàng)目應(yīng)該包括在方括號(hào)中,這樣Python就知道你是在指明一個(gè)列表。一旦你創(chuàng)建了一個(gè)列表,你可以添加、刪除或是搜索列表中的項(xiàng)目。由于你可以增加或刪除項(xiàng)目,我們說列表是 可變的 數(shù)據(jù)類型,即這種類型是可以被改變的。
例:  

輸出    
$python using_list.py
These items are: Linux Nginx MySQL PHP
add Apache.
list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache']
 
I will sort my list now
Sorted list is ['Apache', 'Linux', 'MySQL', 'Nginx', 'PHP']
 
The first item Apache
delete first item
list is now ['Linux', 'MySQL', 'Nginx', 'PHP']

2. 元組
元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號(hào)中用逗號(hào)分割的項(xiàng)目定義。元組通常用在使語句或用戶定義的函數(shù)能夠安全地采用一組值的時(shí)候,即被使用的元組的值不會(huì)改變。
例:    
#!/usr/bin/env python
#coding:utf8
 
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
 
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]

輸出    
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin

3. 字典
字典類似于你通過聯(lián)系人名字查找地址和聯(lián)系人詳細(xì)情況的地址簿,即,我們把鍵(名字)和值(詳細(xì)情況)聯(lián)系在一起。注意,鍵必須是唯一的,就像如果有兩個(gè)人恰巧同名的話,你無法找到正確的信息。

注意,你只能使用不可變的對(duì)象(比如字符串)來作為字典的鍵,但是你可以不可變或可變的對(duì)象作為字典的值?;菊f來就是,你應(yīng)該只使用簡(jiǎn)單的對(duì)象作為鍵。

鍵值對(duì)在字典中以這樣的方式標(biāo)記:d = {key1 : value1, key2 : value2 }。注意它們的鍵/值對(duì)用冒號(hào)分割,而各個(gè)對(duì)用逗號(hào)分割,所有這些都包括在花括號(hào)中。

記住字典中的鍵/值對(duì)是沒有順序的。如果你想要一個(gè)特定的順序,那么你應(yīng)該在使用前自己對(duì)它們排序。

字典是dict類的實(shí)例/對(duì)象。
例:
    
#!/usr/bin/env python
#coding:utf8
 
contacts = { 'Admin' : 'admin@jb51.net',
'Linuxeye' : 'linuxeye@jb51.net',
'Support' : 'support@jb51.net'
}
 
print "Linuxeye's address is %s" % contacts['Linuxeye']
 
# Adding a key/value pair
contacts['test'] = 'test@jb51.net'
 
# Deleting a key/value pair
del contacts['Support']
 
print '\nThere are %d contacts in the address-book\n' % len(contacts)
for name, address in contacts.items():
print 'Contact %s at %s' % (name, address)
 
if contacts.has_key('test'):
print "\ntest's address is %s" % contacts['test']

輸出
    
$ python using_dict.py
Linuxeye's address is linuxeye@jb51.net
 
There are 3 contacts in the address-book
 
Contact Admin at admin@jb51.net
Contact test at test@jb51.net
Contact Linuxeye at linuxeye@jb51.net
 
test's address is test@jb51.net

數(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ù)說明請(qǐng)參見: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); }