"pythonic生物人"的第37 篇分享。
原創(chuàng)不易,點(diǎn)個(gè)“贊“或"在看"鼓勵(lì)下唄 !
摘要
本節(jié)梳理python中另外兩種容器字典(dict)和集合(set)的基本用法。
目錄
1、字典
返回字典中元素個(gè)數(shù)判斷字典是否存在某個(gè)鍵取出鍵對(duì)應(yīng)的值修改鍵對(duì)應(yīng)的值刪除字典的某個(gè)鍵值對(duì)返回某個(gè)鍵對(duì)應(yīng)的值,不存在設(shè)置默認(rèn)值替代刪除鍵對(duì)應(yīng)的值取出字典中所有鍵值對(duì)取出字典中所有鍵返回字典所有鍵組成的列表取出字典中所有值取出字典中的值并將刪除刪除字典中所有的鍵值對(duì)字典淺復(fù)制
2、集合(set)
兩個(gè)集合求并集:使用符號(hào)&或者union函數(shù)兩個(gè)集合求差集:使用符號(hào)-或者difference函數(shù)求兩個(gè)集合中不同時(shí)存在的元素:使用符號(hào)^或者symmetric_difference函數(shù)求集合元素個(gè)數(shù):len(set)成員資格判斷a集合和b集合是否有共同元素(相交):使用isdisjoint判斷a集合是否屬于或等于b集合:使用<=符號(hào),判斷b集合是否包含a集合:使用>=符號(hào),>符號(hào)和issuperset函數(shù)元素x添加到a集合中:a.add(x)移除集合a中元素x:a.remove(x)移除集合a中元素x:a.discard(x)任意移除集合a中的一個(gè)元素:a.pop()清空集合a元素:a.clear()
正文開始啦
1、字典
字典(dict)是python中的映射容器。字典中存儲(chǔ)鍵(key)值(value)對(duì),通過鍵調(diào)用值,鍵具有唯一性,值可以不唯一;每個(gè)鍵值對(duì)之間使用逗號(hào)分隔,鍵與值之間使用頓號(hào)分割;列表、集合、字典因?yàn)榭尚薷乃圆荒茏鳛樽值涞逆I;字符串、數(shù)值、元組不可修改可以作為字典的鍵。
#{}直接創(chuàng)建In 1: {"jack":"man", "Rose":"womman"}Out1: {'jack': 'man', 'Rose': 'womman'}#dict函數(shù)創(chuàng)建In 3: pre_dict =[("Jack","man"), ("Rose", "woman")]In 5: dict(pre_dict) Out5: {'Jack': 'man', 'Rose': 'woman'}#借助zip創(chuàng)建(zip 將可迭代對(duì)象組合成元組列表) In 7: dict(zip(["Jack","Rose"], ["man","woman"])) Out7: {'Jack': 'man', 'Rose': 'woman'}In?22:?{('Jack'):"man",?"Rose":"womman"}#元組('Jack')可以作為鍵Out22: {'Jack': 'man', 'Rose': 'womman'}In 23: {['Jack']:"man", "Rose":"womman"}#列表['Jack']不能作為鍵 TypeError Traceback (most recent call last) in ----> 1 {['Jack']:"man", "Rose":"womman"}TypeError: unhashable type: 'list'
主要介紹一下常見的字典方法。
In [2]: print(dir(dict))
['class', 'contains', 'delattr', 'delitem', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'setitem', 'sizeof', 'str', 'subclasshook', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
len(d),返回字典d中鍵值對(duì)(python中一個(gè)鍵值對(duì)稱為一個(gè)項(xiàng))的個(gè)數(shù)。In [38]: len(a_dict)Out[38]: 2
key in d,存在返回True,不存在返回False。In [49]: "Jack1" in a_dictOut[49]: FalseIn [50]: "Rose" in a_dictOut[50]: True
d[key],當(dāng)key不存在d中時(shí),報(bào)錯(cuò)。In [41]: a_dict["Jack"]Out[41]: 'man'In?[42]:?a_dic["Lucy"]#字典中不存在Lucy,報(bào)錯(cuò)。---------------------------------------------------------------------------NameError Traceback (most recent call last) in----> 1 a_dic["Lucy"]NameError: name 'a_dic' is not defined
d[key] = value。
In [43]: a_dictOut[43]: {'Jack': 'man', 'Rose': 'woman'}In [44]: a_dict["Jack"] = "man1"In [45]: a_dictOut[45]: {'Jack': 'man1', 'Rose': 'woman'}
d.pop(key,value),如果key存在,刪除字典d中key/value鍵值對(duì);key不存在,返回指定的value;如果value沒指定,報(bào)錯(cuò)。In [98]: a_dictOut[98]: {'Jack': 'man', 'Rose': 'woman'}In [99]: a_dict.pop("Jack")Out[99]: 'man'In [100]: a_dictOut[100]: {'Rose': 'woman'}In [101]: a_dict.pop("Jack","Not exit!")Out[101]: 'Not exit!'In [102]: a_dictOut[102]: {'Rose': 'woman'}
d.get(key,value),當(dāng)d中存在key時(shí),返回key對(duì)應(yīng)的value;當(dāng)d中不存在key時(shí),返回指定的value,不指定value返回None。In [56]: a_dictOut[56]: {'Rose': 'woman'}In [58]: a_dict.get("Rose","haha")#Rose存在,返回對(duì)應(yīng)值。Out[58]: 'woman'In [59]: a_dict.get("Lucy","haha")#Lucy不存在,返回指定value "haha"。Out[59]: 'haha'In [60]: print(a_dict.get("Lucy"))#Lucy不存在,返回None。Out[60]: None
del d[key],字典不存在key,報(bào)錯(cuò)退出。In [43]: a_dictOut[43]: {'Jack': 'man', 'Rose': 'woman'}In [44]: a_dict["Jack"] = "man1"In [45]: a_dictOut[45]: {'Jack': 'man1', 'Rose': 'woman'}In [46]: del a_dict["Jack"]In [47]: a_dictOut[47]: {'Rose': 'woman'}In [48]: del a_dict["Jack1"]#不存在鍵Jack1,報(bào)錯(cuò)退出。---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 del a_dict["Jack1"]KeyError: 'Jack1'
d.items()In [74]: a_dictOut[74]: {'Rose': 'woman', 'Jack': 'man'}In [75]: a_dict.items()#items返回字典鍵值對(duì)視圖對(duì)象,支持迭代,通過list轉(zhuǎn)化為列表。Out[75]: dict_items([('Rose', 'woman'), ('Jack', 'man')])In [76]: list(a_dict.items())Out[76]: [('Rose', 'woman'), ('Jack', 'man')]
d.keys()In [77]: a_dict.keys()Out[77]: dict_keys(['Rose', 'Jack'])In [78]: list(a_dict.keys())Out[78]: ['Rose', 'Jack']
list(d)In [30]: a_dictOut[30]: {'Jack': 'man', 'Rose': 'woman'}In [31]: list(a_dict)Out[31]: ['Jack', 'Rose']
d.values()In [79]: a_dict.values()Out[79]: dict_values(['woman', 'man'])In [80]: list(a_dict.values())Out[80]: ['woman', 'man']
d.pop(key),返回key對(duì)應(yīng)的鍵,同時(shí)刪除該鍵值對(duì)。In [81]: a_dictOut[81]: {'Rose': 'woman', 'Jack': 'man'}In [82]: a_dict.pop("Rose")Out[82]: 'woman'In [83]: a_dictOut[83]: {'Jack': 'man'}
d.clear(),刪除字典中所有的鍵值對(duì),返回空字典{}。In [85]: a_dictOut[85]: {'Jack': 'man'}In [86]: a_dict.clear()In [87]: a_dictOut[87]: {}
d.copy()In [89]: a_dictOut[89]: {'Jack': 'man'}In [91]: b_dict = a_dict.copy()In [92]: b_dictOut[92]: {'Jack': 'man'}In [93]: b_dict["Jack"] = "man1"In [94]: b_dictOut[94]: {'Jack': 'man1'}In [95]: a_dictOut[95]: {'Jack': 'man'}
2、集合(set) 集合是非序列和映射python容器,集合中元素?zé)o序,可以理解為只有鍵的字典(集合中不可能有重復(fù)元素 );
集合并不記錄元素索引,所以集合不支持索引、切片或其他序列類的操作;
python內(nèi)置了兩種集合類型,set類型可修改,forezenset類型不可變,常見的用途 :
成員檢測(cè)(in);序列容器去除重復(fù)項(xiàng);求交集、并集、差集與對(duì)稱差集。
In [116]: {'a', 'b', 'c', 'd', 'e'}#花括號(hào){}包圍創(chuàng)建Out[116]: {'a', 'b', 'c', 'd', 'e'}In [117]: set("abcde")#set函數(shù)創(chuàng)建Out[117]: {'a', 'b', 'c', 'd', 'e'}**注意:**空的{}表示字典,而不是集合。
集合使用 兩個(gè)集合求交集:使用&號(hào)或者intersection函數(shù)。 In [128]: a_setOut[128]: {'a', 'b', 'c', 'd'}In [129]: b_setOut[129]: {'a', 'b', 'c', 'd', 'e'}In [130]: a_set & b_set #使用&號(hào)求交集Out[130]: {'a', 'b', 'c', 'd'}In [131]: a_set.intersection(b_set)#intersection函數(shù)求交集Out[131]: {'a', 'b', 'c', 'd'}
In [132]: a_set | b_set#使用符號(hào)|Out[132]: {'a', 'b', 'c', 'd', 'e'}In [138]: a_set.union(b_set)#使用union函數(shù)Out[138]: {'a', 'b', 'c', 'd', 'e'}
In [142]: a_set - b_set#使用-符號(hào)。返回a_set中存在,而b_set中不存在的一個(gè)新集合Out[142]: set()In [143]: b_set - a_set#返回b_set中存在,而a_set中不存在的一個(gè)新集合Out[143]: {'e'}In [144]: a_set.difference(b_set)#使用difference函數(shù)。返回a_set中存在,而b_set中不存在的一個(gè)新集合Out[144]: set()In [145]: b_set.difference(a_set)Out[145]: {'e'}
In [146]: a_set ^ b_setOut[146]: {'e'}In [147]: a_set.symmetric_difference(b_set)Out[147]: {'e'}
In [148]: len(a_set)Out[148]: 4
In [148]: len(a_set)Out[148]: 4In [149]: "a" in a_setOut[149]: TrueIn [150]: "x" in a_setOut[150]: False
有共同元素返回False,否則返回True。In [151]: a_set.isdisjoint(b_set)Out[151]: FalseIn [152]: b_set.isdisjoint(a_set)Out[152]: FalseIn [153]: a_set.isdisjoint(["x"])Out[153]: True
In [155]: a_set.issubset(b_set)#使用函數(shù)issubset,a_set完全屬于b_setOut[155]: TrueIn [156]: a_set <= b_set#使用符號(hào)<=Out[156]: True
In [162]: b_set.issuperset(a_set)Out[162]: TrueIn [163]: b_set >= a_setOut[163]: True
如果x已經(jīng)存在a中,沒任何效果;否則加入。In [171]: a_setOut[171]: {'a', 'b', 'c', 'd'}In [172]: a_set.add("a")#a已存在無任何效果In [173]: a_set.add("x")#添加xIn [174]: a_setOut[174]: {'a', 'b', 'c', 'd', 'x'}
如果 x 不存在于a中則報(bào)錯(cuò)。In [175]: a_setOut[175]: {'a', 'b', 'c', 'd', 'x'}In [176]: a_set.remove("y")#y不存在集合a_set中---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 a_set.remove("y")KeyError: 'y'In [177]: a_set.remove("x")In [178]: a_setOut[178]: {'a', 'b', 'c', 'd'}
如果元素x 存在于集合a中則將其移除;不存在沒任何效果,與remove有細(xì)微差別。In [178]: a_setOut[178]: {'a', 'b', 'c', 'd'}In [179]: a_set.discard("x")#a_set不存在x元素,沒任何效果,區(qū)別于remove函數(shù)In [180]: a_set.discard("d")In [181]: a_setOut[181]: {'a', 'b', 'c'}
如果集合為空則會(huì)報(bào)錯(cuò)。In [183]: a_setOut[183]: {'a', 'b', 'c'}In [184]: a_set.pop()Out[184]: 'a'In [185]: a_set.pop()Out[185]: 'c'In [186]: a_set.pop()Out[186]: 'b'In [187]: a_set.pop()---------------------------------------------------------------------------KeyError Traceback (most recent call last) in----> 1 a_set.pop()KeyError: 'pop from an empty set'In [188]: a_set#集合此時(shí)已經(jīng)為空Out[188]: set()
In [190]: a_setOut[190]: {'a', 'b', 'c', 'd'}In [191]: a_set.clear()In [192]: a_setOut[192]: set()
同系列文章 python3基礎(chǔ)01數(shù)值和字符串(一)
python3基礎(chǔ)02數(shù)值和字符串(二)
python3基礎(chǔ)03列表(list)和元組(tuple)
原創(chuàng)不易 "點(diǎn)贊"、"在看" 鼓 勵(lì)下唄 !
總結(jié)
以上是生活随笔 為你收集整理的winform list集合怎么 in过滤_python3基础04字典(dict)和集合(set) 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。