У меня есть словари внутри списка как: -
L= [{'id': 3, 'term': 'bugatti', 'bucket_id': 'ad_3'},
{'id': 4, 'term': 'mercedez', 'bucket_id': 'ad_4'},
{'id': 8, 'term': 'entertainment', 'bucket_id': 'ad_8'},
{'id': 8, 'term': 'entertainment', 'bucket_id': 'ad_8'},
{'id': 9, 'term': 'music', 'bucket_id': 'ad_9'}]
, а другой список как: -
words=['bugatti', 'entertainment', 'music','politics']
Все, что я хочу отобразить элементы списка words
с ключом term
и хочет получить соответствующий словарь.Ожидаемый результат:
new_list= [{'id': 3, 'term': 'bugatti', 'bucket_id': 'ad_3'},
{'id': 8, 'term': 'entertainment', 'bucket_id': 'ad_8'},
{'id': 8, 'term': 'entertainment', 'bucket_id': 'ad_8'},
{'id': 9, 'term': 'music', 'bucket_id': 'ad_9'}]
То, что я пробовал как:
for d in L:
for k,v in d.items():
for w in words:
if v==w:
print (k,v)
, дает мне только:
term bugatti
term entertainment
term entertainemnt
term music