正在尝试将列表的元素添加到列表字典中,我已尽力而为,但无法完成。
字典列表
fortunne_500=[{'Rank': 1,
'Company': 'Walmart',
'Country': 'United States',
'Industry': 'Retail',
'Revenue in USD': '$500 billion'},
{'Rank': 2,
'Company': 'State Grid',
'Country': 'China',
'Industry': 'Power',
'Revenue in USD': '$349 billion'},
{'Rank': 3,
'Company': 'Sinopec Group',
'Country': 'China',
'Industry': 'Petroleum',
'Revenue in USD': '$327 billion'}]
列表
rev_per_emp = [217391.30434782608,376142.84374767606,911953.2812190612]
所需的输出
[{'Rank': 1,
'Company': 'Walmart',
'Country': 'United States',
'Industry': 'Retail',
'Revenue in USD': '$500 billion',
'rev per emp': 217391.30434782608},
{'Rank': 2,
'Company': 'State Grid',
'Country': 'China',
'Industry': 'Power',
'Revenue in USD': '$349 billion',
'rev per emp': 376142.84374767606},
{'Rank': 3,
'Company': 'Sinopec Group',
'Country': 'China',
'Industry': 'Petroleum',
'Revenue in USD': '$327 billion',
'rev per emp': 911953.2812190612}]
我将很高兴为您提供帮助。谢谢
这是您需要的:)
for fortunne_dict,rpe in zip(fortunne_500,rev_per_emp):
fortunne_dict['rev per emp'] = rpe
可以这样做:
for i in range(len(fortunne_500)):
fortunne_500[i]["ref per emp"]=ref_per_emp[i]
当然,您必须确保两个列表的大小相同
使用这样的枚举器
for i, item in enumerate(fortunne_500):
item["rev per emp"] = rev_per_emp[i]
这将使它:
for ix, val in enumerate(rev_per_emp):
fortunne_500[ix].update({"rev per emp": val})
print(fortunne_500)
您可以尝试以下方法:
myit = iter(rev_per_emp)
for dict in fortunne_500:
dict['rev_per_emp'] = next(myit)