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

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'

你可能感兴趣的:(python,测试工具,自动化测试,python)