python字典和集合总结
下面是小编为大家整理的python字典和集合总结,供大家参考。
字典总结¶ 字典是⼀种key-value结构 字典的key值必须不可变,也就是可hash,并且唯⼀ 字典的value值可以是任意类型,并且可以重复 字典是⽆顺序的 字典的查找速度快定义字典¶In [2]:dict1 = {"zhangsan":18,"lisi":19,"wangwu":25}In [3]:dict1Out[3]:{"zhangsan": 18, "lisi": 19, "wangwu": 25}字典的查找¶查找lisi,⽅式⼀In [6]:dict1["lisi"]Out[6]:19查找⼀个不存在的key值,会报错In [5]:dict1["mayun"]---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-5-ec7fecd24d78> in <module>----> 1 dict1["mayun"]KeyError: "mayun"查找lisi的⽅式⼆:In [7]:dict1.get("lisi")Out[7]:19查找⼀个不存在的值,不会报错In [8]:dict1.get("mayun")字典的增加¶In [9]:
dict1Out[9]:{"zhangsan": 18, "lisi": 19, "wangwu": 25}In [10]:dict1["mahuteng"] = 40In [11]:dict1Out[11]:{"zhangsan": 18, "lisi": 19, "wangwu": 25, "mahuteng": 40}字典的删除¶⽅式⼀:In [12]:dict1Out[12]:{"zhangsan": 18, "lisi": 19, "wangwu": 25, "mahuteng": 40}In [13]:dict1.pop("wangwu")Out[13]:25In [14]:dict1Out[14]:{"zhangsan": 18, "lisi": 19, "mahuteng": 40}⽅式⼆:随机删除In [18]:dict1Out[18]:{"zhangsan": 18, "lisi": 19}In [19]:dict1.popitem()Out[19]:("lisi", 19)In [20]:dict1Out[20]:{"zhangsan": 18}⽅式三:全局删除
In [21]:dict1.clear()In [22]:dict1Out[22]:{}字典的其它的⽅法¶In [23]:dict2 = {1:2,2:3,3:4,4:5}In [24]:dict2Out[24]:{1: 2, 2: 3, 3: 4, 4: 5}In [25]:dict2.keys()Out[25]:dict_keys([1, 2, 3, 4])In [26]:dict2.values()Out[26]:dict_values([2, 3, 4, 5])In [28]:dict2.items()Out[28]:dict_items([(1, 2), (2, 3), (3, 4), (4, 5)])字典的update⽅法In [30]:dict1 = {"zhangsan": 28,"lisi":18,"wangwu":30}dict2 = {"zhangsan":100,"lisi":18,"mayun":38}In [31]:dict1.update(dict2)dict1Out[31]:{"zhangsan": 100, "lisi": 18, "wangwu": 30, "mayun": 38}字典的循环¶In [32]:dict1 = {1:2,2:3,3:4,4:5}循环打印key值
In [34]:for k in dict1:print(k)1234同时打印出key值和value值,有两种⽅式:In [36]:for k in dict1:print(k,dict1[k])1 22 33 44 5In [37]:for k,v in dict1.items():print(k,v)1 22 33 44 5集合总结¶ 集合是不可以重复的 集合是没有顺序的 集合⾥的元素不可变,也就是可hash定义集合¶In [40]:set1 = {1,2,3,4,5,6}set1Out[40]:{1, 2, 3, 4, 5, 6}In [41]:set2 = {1,2,3,3,4,5,5,6,6}set2Out[41]:{1, 2, 3, 4, 5, 6}In [42]:set3 = {1,2,3,[1,2,3]}set3
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-42-283fa1f4b424>in <module>----> 1 set3 = {1,2,3,[1,2,3]} 2 set3TypeError: unhashable type: "list"集合的两⼤作⽤:1. 去重 2.关系测试¶去重In [43]:list1 = [1,2,3,3,4,4,5,5,6,7,8,8]set1 = set(list1)set1Out[43]:{1, 2, 3, 4, 5, 6, 7, 8}关系测试In [44]:iphone7 = {"张三","李四","王五","赵六"}iphone8 = {"张三","李四","孙权","⻄⻔庆"}找同时买 iphone7和 iphone8 的,也就是求交集¶In [45]:iphone7.intersection(iphone8)Out[45]:{"张三", "李四"}In [46]:iphone7 & iphone8Out[46]:{"张三", "李四"}找只买 iphone7的或者只买 iphone8,也就是求差集¶In [47]:iphone7.difference(iphone8)Out[47]:{"王五", "赵六"}In [48]:iphone8.difference(iphone7)Out[48]:{"孙权", "⻄⻔庆"}In [49]:iphone7 - iphone8Out[49]:{"王五", "赵六"}In [50]:iphone8 - iphone7
Out[50]:{"孙权", "⻄⻔庆"}求并集¶In [51]:iphone7.union(iphone8)Out[51]:{"孙权", "张三", "李四", "王五", "⻄⻔庆", "赵六"}In [52]:iphone7 | iphone8Out[52]:{"孙权", "张三", "李四", "王五", "⻄⻔庆", "赵六"}只买 iphone7的和只买 iphone8 的,也就是求对称差集¶In [53]:iphone7.symmetric_difference(iphone8)Out[53]:{"孙权", "王五", "⻄⻔庆", "赵六"}In [54]:iphone7 ^ iphone8Out[54]:{"孙权", "王五", "⻄⻔庆", "赵六"}判断⼦集和超集¶In [55]:set1 = {1,2,3,4,5}set2 = {1,2,3,4,5,6}In [56]:set1.issubset(set2)Out[56]:TrueIn [57]:set2.issubset(set1)Out[57]:FalseIn [58]:set2.issuperset(set1)Out[58]:TrueIn [ ]:
推荐访问:python总结心得体会 python字典和集合总结 字典 集合 python
版权声明:
1.世霆文库网的资料来自互联网以及用户的投稿,用于非商业性学习目的免费阅览。
2.《python字典和集合总结》一文的著作权归原作者所有,仅供学习参考,转载或引用时请保留版权信息。
3.如果本网所转载内容不慎侵犯了您的权益,请联系我们,我们将会及时删除。