您好,登錄后才能下訂單哦!
一、元組的常用方法
①元組方法-------count(),元組中某個元素出現的次數
>>> 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(),查找出元組中元素的下標位置,如果元素不存在,則會報錯
>>> 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(),清除字典內容
>>> name_dict={'alex':22,'eric':26,'tony':25} >>> name_dict {'tony': 25, 'alex': 22, 'eric': 26} >>> name_dict.clear() >>> name_dict {} >>>
淺拷貝
②字典常用方法-------copy(),字典淺拷貝,該拷貝紙拷貝第一層,如果子點的key或者value下還繼續有字典,
>>> 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(被拷貝的字典),拷貝多層,當被拷貝的字典中二層或者二層以上中的鍵對應的值發生變化,通過該方法拷貝得到的字典test002_dict中的鍵值是不會發生變化的
為什么要拷貝?
1當進行修改時,想要保留原來的數據和修改后的數據
數字字符串 和 集合 在修改時的差異? (深淺拷貝不同的終極原因)
1在修改數據時:
2數字字符串:在內存中新建一份數據
3集合:修改內存中的同一份數據
對于集合,如何保留其修改前和修改后的數據?
1在內存中拷貝一份
③字典常用方法-------get(),獲取某個key對應的值,如果該key不存在,返回None,也可以指定返回其他結果,get方法可以避免在key不存在的時候返回報錯
>>> 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' >>>
④字典一般用字符串,數字,類的實例來作為key
⑤判斷一個字典是否為字典用如下方法:
>>> nu_dict = {'alex':18,'tony':22} >>> type(nu_dict)<type 'dict'> >>> type(nu_dict) is dictTrue >>>
⑥字典常用方法-------fromkeys(),可以將一個列表與定義的后面的一個值進行匹配,生成一個新的字典,該方法可以用于一個人名列表,如果要把人名列表對應每個人的個人信息,可以用到次方法
>>>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(),取出字典中所有對應的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)] >>>
對于字典中的內容不是很多,即上百條或者上千條更甚至上萬條可以用以上方法,如果上百萬或千萬條記錄,則建議用如下方法:
>>> for k in name_dict:print k,name_dict[k] ... tony 18 alex 22 eric 24 >>>
這樣好處是只讀取了字典中的key,生成一個列表,然后再拿著key去字典中找對應的value,可以節約內存,而上面的方法是在內存中生成兩個列表,這樣就消耗了過多的內存資源。
⑧字典常用方法-------keys(),當前所有的key打印出來
>>> name_dict{'tony': 18, 'alex': 22, 'eric': 24}>>> name_dict.keys()['tony', 'alex', 'eric']>>>
⑨字典常用方法-------pop(),刪除指定的鍵值對,只需要指定鍵即可
>>> name_dict {'tony': 18, 'alex': 22, 'eric': 24} >>> name_dict.pop('alex') 22 >>> name_dict {'tony': 18, 'eric': 24} >>>
⑩字典常用方法-------del,該方法是一個全局性的,可以刪除一個字典,列表,變量,元組等
>>> 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()如果字典的鍵對應的值存在,則輸出,不存在則定義一個默認值
>>> 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(),將兩個字典進行整合
>>> 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中的鍵有重復,則以tt中的鍵所對應的值替換掉ss中keys對應的值,如果tt中的鍵值對在ss中不存在,則在ss中進行創建
字典常用方法-------values(),打印字典中所有的值
>>> ss {'a': 'kk', 'rt': 33, 'pp': 23, 'bb': 22, 'cc': 33} >>> ss.values() ['kk', 33, 23, 22, 33] >>>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。