Skip to main content
 首页 » 编程设计

python之在 python 中重新组合这两个列表的聪明方法

2025年05月04日8傻小

我在 python 中有以下列表,我想将它们重新组合成字典/列表:

names = ['banana','grapefruit','apple'] 
colors = ['yellow','pink','green'] 

到

fruits = [ 
{'name':'banana','color':'yellow'},  
{'name':'grapefruit','color':'pink'}, 
{'name':'apple','color':'green'} 
] 

最好的方法是什么?我目前正绞尽脑汁想写循环。

请帮忙! :)

请您参考如下方法:

fruits = [{"name": name, "color": color} for name, color in zip(names, colors)]