我可以这样做:
filter(lambda x: x.key1 in ["aa", "bb", "cc"], [{key1: ..., key2: ...}, {key1: ...}])
我怎么能做相反的事情?
dict_items = [{key1: ..., key2: ...}, {key1: ...}]
filter(lambda x: x in ???dict_items.key1???, ["aa", "bb", "cc"])
答案 0 :(得分:0)
假设您有两个dicts d1 = {key1: val1, key2: val2
}和d2 = {key3: val3, key4: val4}
目前尚不清楚您要比较的是什么,但如果您想要比较键,请输入:
set.intersection(set(d1.keys()), set(d2.keys()))
对于值:
set.intersection(set(d1.values()), set(d2.values()))
答案 1 :(得分:0)
use可以使用list comprehension如下:
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"]]
或者如果您只需要钥匙,可以使用:
print [key for j in [a, b] for key in j.keys() if key in ["aa", "bb", "cc"]]
输出:
['aa', 'bb']