[英]How to check if a value of a keys of an array exist in another array?
I can do this: 我可以做这个:
filter(lambda x: x.key1 in ["aa", "bb", "cc"], [{key1: ..., key2: ...}, {key1: ...}])
How can I do the opposite thing? 我怎么能做相反的事情呢?
dict_items = [{key1: ..., key2: ...}, {key1: ...}]
filter(lambda x: x in ???dict_items.key1???, ["aa", "bb", "cc"])
Assuming you have two dicts d1 = {key1: val1, key2: val2
} and d2 = {key3: val3, key4: val4}
假设你有两个dicts d1 = {key1: val1, key2: val2
}和d2 = {key3: val3, key4: val4}
It's not clear what you are trying to compare but if you want to compare keys just type: 目前还不清楚你要比较什么,但如果你想比较键只需输入:
set.intersection(set(d1.keys()), set(d2.keys()))
For values: 对于价值观:
set.intersection(set(d1.values()), set(d2.values()))
use can use list comprehension as follows: 使用可以使用列表理解如下:
a = {"aa":1, "ll":4}
b = {"bb": 'pl', "xx":12, "qq": 66}
print [key for j in [a, b] for key, val in j.iteritems() if key in ["aa", "bb", "cc"]]
Or if you only require keys u can use: 或者,如果您只需要钥匙,您可以使用:
print [key for j in [a, b] for key in j.keys() if key in ["aa", "bb", "cc"]]
output: 输出:
['aa', 'bb']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.