一、元組的常用方法
①元組方法-------count(),元組中某個(gè)元素出現(xiàn)的次數(shù)
>>> t_tuple=('a','b',11,22) >>> t_tuple.count('a') 1 >>> t_tuple.count('b') 1 >>> t_tuple.count('11') 0 >>> t_tuple.count(11) 1 >>>
①元組方法-------index(),查找出元組中元素的下標(biāo)位置,如果元素不存在,則會(huì)報(bào)錯(cuò)
>>> t_tuple.index('a')0>>> t_tuple.index('b')1>>> t_tuple.index(11)2>>> t_tuple.index('c')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: tuple.index(x): x not in tuple>>>
二、字典及常用方法
①字典常用方法-------clear(),清除字典內(nèi)容
>>> name_dict={'alex':22,'eric':26,'tony':25} >>> name_dict {'tony': 25, 'alex': 22, 'eric': 26} >>> name_dict.clear() >>> name_dict {} >>>
淺拷貝
②字典常用方法-------copy(),字典淺拷貝,該拷貝紙拷貝第一層,如果子點(diǎn)的key或者value下還繼續(xù)有字典,
>>> name_dict={'alex':22,'eric':26,'tony':25}>>> name_dict.copy(){'tony': 25, 'alex': 22, 'eric': 26}>>> name_dicts = name_dict.copy()>>> print name_dicts{'tony': 25, 'alex': 22, 'eric': 26}>>> id(name_dict)39525232>>> id(name_dicts)39524800>>>
深拷貝
import copy
test_dict = {'a':{'b':{'c':100}}}
test02_dict=copy.deepcopy(被拷貝的字典),拷貝多層,當(dāng)被拷貝的字典中二層或者二層以上中的鍵對(duì)應(yīng)的值發(fā)生變化,通過(guò)該方法拷貝得到的字典test002_dict中的鍵值是不會(huì)發(fā)生變化的
為什么要拷貝?
1當(dāng)進(jìn)行修改時(shí),想要保留原來(lái)的數(shù)據(jù)和修改后的數(shù)據(jù)
數(shù)字字符串 和 集合 在修改時(shí)的差異? (深淺拷貝不同的終極原因)
1在修改數(shù)據(jù)時(shí):
2數(shù)字字符串:在內(nèi)存中新建一份數(shù)據(jù)
3集合:修改內(nèi)存中的同一份數(shù)據(jù)
對(duì)于集合,如何保留其修改前和修改后的數(shù)據(jù)?
1在內(nèi)存中拷貝一份
③字典常用方法-------get(),獲取某個(gè)key對(duì)應(yīng)的值,如果該key不存在,返回None,也可以指定返回其他結(jié)果,get方法可以避免在key不存在的時(shí)候返回報(bào)錯(cuò)
>>> name_dict {'tony': 25, 'alex': 22, 'eric': 26} >>> name_dict.get('alex') 22 >>> name_dict['alex'] 22 >>> name_dict.get('eric') 26 >>> name_dict.get('susan') >>> print name_dict.get('susan') None >>> print name_dict.get('susan','OK') OK >>> name_dict.get('susan','OK') 'OK' >>> print name_dict['susan'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'susan' >>>
④字典一般用字符串,數(shù)字,類(lèi)的實(shí)例來(lái)作為key
⑤判斷一個(gè)字典是否為字典用如下方法:
>>> nu_dict = {'alex':18,'tony':22} >>> type(nu_dict)<type 'dict'> >>> type(nu_dict) is dictTrue >>>
⑥字典常用方法-------fromkeys(),可以將一個(gè)列表與定義的后面的一個(gè)值進(jìn)行匹配,生成一個(gè)新的字典,該方法可以用于一個(gè)人名列表,如果要把人名列表對(duì)應(yīng)每個(gè)人的個(gè)人信息,可以用到次方法
>>>a{'a': 1, 'c': 3, 'b': 2} >>>a.fromkeys([1,2,3,4,5],'t'){1: 't', 2: 't', 3: 't', 4: 't', 5: 't'} >>> >>>a.fromkeys(['alex','eric','tony','susan'],[]){'tony': [], 'alex': [], 'eric': [], 'susan': []}
⑦字典常用方法-------items(),取出字典中所有對(duì)應(yīng)的key:value
>>>name_dict = {'alex':22,'eric':24,'tony':18} >>>name_dict {'tony': 18, 'alex': 22, 'eric': 24} >>>name_dict.items() [('tony', 18), ('alex', 22), ('eric', 24)] >>>
對(duì)于字典中的內(nèi)容不是很多,即上百條或者上千條更甚至上萬(wàn)條可以用以上方法,如果上百萬(wàn)或千萬(wàn)條記錄,則建議用如下方法:
>>> for k in name_dict:print k,name_dict[k] ... tony 18 alex 22 eric 24 >>>
這樣好處是只讀取了字典中的key,生成一個(gè)列表,然后再拿著key去字典中找對(duì)應(yīng)的value,可以節(jié)約內(nèi)存,而上面的方法是在內(nèi)存中生成兩個(gè)列表,這樣就消耗了過(guò)多的內(nèi)存資源。
⑧字典常用方法-------keys(),當(dāng)前所有的key打印出來(lái)
>>> name_dict{'tony': 18, 'alex': 22, 'eric': 24}>>> name_dict.keys()['tony', 'alex', 'eric']>>>
⑨字典常用方法-------pop(),刪除指定的鍵值對(duì),只需要指定鍵即可
>>> name_dict {'tony': 18, 'alex': 22, 'eric': 24} >>> name_dict.pop('alex') 22 >>> name_dict {'tony': 18, 'eric': 24} >>>
⑩字典常用方法-------del,該方法是一個(gè)全局性的,可以刪除一個(gè)字典,列表,變量,元組等
>>> a='alex' >>> del a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> b = {'ccc':111,'ddd':222} >>> del b >>> b Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'b' is not defined >>> c = ('a',2,'c','f',22,) >>> c ('a', 2, 'c', 'f', 22) >>> del c >>> c Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'c' is not defined >>>
字典常用方法-------setdefault()如果字典的鍵對(duì)應(yīng)的值存在,則輸出,不存在則定義一個(gè)默認(rèn)值
>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: []}>>> c.setdefault(8)[]>>> c.setdefault(200)>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: [], 200: None}>>> c.setdefault(200,'cccc')>>> c{7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [], 21: [], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [], 28: [], 29: [], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [], 37: [], 38: [], 39: [], 40: [], 41: [], 42: [], 43: [], 44: [], 45: [], 46: [], 47: [], 48: [], 49: [], 50: [], 51: [], 52: [], 53: [], 54: [], 55: [], 56: [], 57: [], 58: [], 59: [], 60: [], 61: [], 62: [], 63: [], 64: [], 65: [], 66: [], 67: [], 68: [], 69: [], 70: [], 71: [], 72: [], 73: [], 74: [], 75: [], 76: [], 77: [], 78: [], 79: [], 80: [], 81: [], 82: [], 83: [], 84: [], 85: [], 86: [], 87: [], 88: [], 89: [], 90: [], 91: [], 92: [], 93: [], 94: [], 95: [], 96: [], 97: [], 98: [], 99: [], 200: None}>>>
字典常用方法-------update(),將兩個(gè)字典進(jìn)行整合
>>> ss = {'a':11,'bb':22,'cc':33} >>> tt = {'a':'kk','pp':23,'rt':33} >>> ss.update(tt) >>> ss {'a': 'kk', 'rt': 33, 'pp': 23, 'bb': 22, 'cc': 33} >>>
如果ss中的鍵和tt中的鍵有重復(fù),則以tt中的鍵所對(duì)應(yīng)的值替換掉ss中keys對(duì)應(yīng)的值,如果tt中的鍵值對(duì)在ss中不存在,則在ss中進(jìn)行創(chuàng)建
字典常用方法-------values(),打印字典中所有的值
>>> ss {'a': 'kk', 'rt': 33, 'pp': 23, 'bb': 22, 'cc': 33} >>> ss.values() ['kk', 33, 23, 22, 33] >>>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:元組、字典的常用方法-創(chuàng)新互聯(lián)
瀏覽地址:http://www.rwnh.cn/article8/ccgoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、Google、網(wǎng)站設(shè)計(jì)、外貿(mào)建站、微信小程序、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容