我有两个清单:
value1
我想比较这两个列表,如果b
匹配,那么它将返回列表c= ['x','z']
的所有键。我想要一个这样的结果:
cost-1
答案 0 :(得分:1)
我将其读作:"从列表k
中的词典d
返回所有键b
的列表,以便k
的值d
中的{}与列表a
"
val_set = {v for d in a for v in d.values()}
output = [k for d in b for k, v in d.items() if v in val_set]
print(output)
#['x', 'z']
答案 1 :(得分:0)
您可以实现以下内容:
a = [{'1':'value1'},{'2':'value2'}]
b = [{'x':'value1'},{'y':'hello'},{'z':'value1'}]
c = [w.keys()[0] for q in a for w in b if q.values()[0] == w.values()[0]]
print(c)
输出:
['x', 'z']
相当于:
a = [{'1':'value1'},{'2':'value2'}]
b = [{'x':'value1'},{'y':'hello'},{'z':'value1'}]
c = []
for q in a:
for w in b:
if q.values()[0] == w.values()[0]:
c.append(w.keys()[0])
print(c)