A ={"a":1,"b":2,"c":3}
B ={"a":11,"b":2,"d":100}print(A.keys()& B.keys())# 获取相同的键print(A.keys()- B.keys())# 获取不同的键print(A.items()& B.items())# 获取相同的键值对
“”“
输出结果
{'a','b'}{'c'}{('b',2)}
“”“
itemgetter
1、函数itemgetter()是operator模块中的内置函数
2、功能是获取对象中指定域的值,参数为一些序号(即,需要获取的数据在对象中的序号),例如:
from operator import itemgetter
a =[1,2,3]
b = itemgetter(1)# 定义函数b,获取对象的第1个域的值print(b(a))# 2
b = itemgetter(1,0)# 定义函数b,获取对象的第1个和第0个域的值print(b(a))# (2, 1)