python 字典支持交集和并集操作

来源:互联网 发布:office卸载软件 编辑:程序博客网 时间:2024/06/08 02:58
d=dict(a=1,b=2,c=3)k=d.keys()   #dict_keys(['a', 'b', 'c'])del d['a']print(k)    #k会动态变化 dict_keys(['b', 'c'])#字典的key是唯一的,支持交集和并集操作,d.values()是不可以的print(k|{'x':4})    #{'c', 'b', 'x'}print(k&{'x':4})    #set()print(d.items()|{'x':4})    #{'x', ('b', 2), ('c', 3)}print(d.items()&{'x':4})    #set()print(d.values()|{'x':4})   #报错TypeError: unsupported operand type(s) for |: 'dict_values' and 'dict'
原创粉丝点击