简体   繁体   English

我如何知道 python 的两个或多个字典中是否有任何“相等的值”?

[英]How do I know if there are any 'equal values' in two or more dictionaries in python?

Is there any pythonic way to find out this?有什么pythonic方法可以找出这个吗?

dic1 = {'a': 1, 'b': 2, 'points': 100}
dic2 = {'a': 10, 'b': 20, 'points': 100}

---> Ture  # because points in dic1 and dic2 are equal

and also并且

dic1 = {'a': 1, 'b': 2, 'points': 200}
dic2 = {'a': 10, 'b': 20, 'points': 100}

---> False # because points in dic1 and dic2 are not equal

You can find a way with this post: Comparing two dictionaries and checking how many (key, value) pairs are equal您可以通过这篇文章找到一种方法: 比较两个字典并检查有多少(键,值)对是相等的

you'll have to check the length of:您必须检查以下内容的长度:

shared_items = {k: dic1[k] for k in dic1 if k in dic2 and dic1[k] == dic2[k]}
print len(shared_items)

Change it a little to avoid error if there is not the same keys in both dicts如果两个字典中的键不同,请稍作更改以避免错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM