【发布时间】:2019-02-24 08:44:25
【问题描述】:
我在列表中有字典:-
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']
我想用键term 映射列表words 的元素,并想得到相应的字典。预期输出为:
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
【问题讨论】:
标签: python arrays python-3.x dictionary key