Python字典交集–比较两个字典

  1. Python基础教程
  2. 在SublimeEditor中配置Python环境
  3. Python代码中添加注释
  4. Python中的变量的使用
  5. Python中的数据类型
  6. Python中的关键字
  7. Python字符串操作
  8. Python中的list操作
  9. Python中的Tuple操作
  10. Pythonmax()和min()–在列表或数组中查找最大值和最小值
  11. Python找到最大的N个(前N个)或最小的N个项目
  12. Python读写CSV文件
  13. Python中使用httplib2–HTTPGET和POST示例
  14. Python将tuple开箱为变量或参数
  15. Python开箱Tuple–太多值无法解压
  16. Pythonmultidict示例–将单个键映射到字典中的多个值
  17. PythonOrderedDict–有序字典
  18. Python字典交集–比较两个字典
  19. Python优先级队列示例

Python示例,用于查找2个或更多词典之间的常见项目,即字典相交项目。

1.使用“&”运算符的字典交集

最简单的方法是查找键,值或项的交集,即 & 在两个字典之间使用运算符。

example.py

a = { 'x' : 1, 'y' : 2, 'z' : 3 }
b = { 'u' : 1, 'v' : 2, 'w' : 3, 'x'  : 1, 'y': 2 }
set( a.keys() ) & set( b.keys() ) # Output set(['y', 'x'])

set( a.items() ) & set( b.items() ) # Output set([('y', 2), ('x', 1)])

2.设置交集()方法

Set intersection()方法返回一个集合,其中包含集合a和集合b中都存在的项。

example.py

a = { 'x' : 1, 'y' : 2, 'z' : 3 }

b = { 'u' : 1, 'v' : 2, 'w' : 3, 'x'  : 1, 'y': 2 }

setA = set( a )

setB = set( b )

setA.intersection( setB ) 

# Output set(['y', 'x'])

for item in setA.intersection(setB):
	print item
	
#x
#y

请把与检查两个字典在python中是否具有相同的键或值有关的问题交给我。

学习愉快!

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×