在python中重新组合这两个列表的聪明方法 [英] the smart way to recombine these two lists in python

查看:166
本文介绍了在python中重新组合这两个列表的聪明方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have the following lists in python which i want to recombine into a dictionary/list:

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

to

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

最好的方法是什么?我正在把我的大脑打结,试图写回圈。

what's the best way to do it? i'm currently tying my brain up in knots trying to write loops.

请帮忙! :)

推荐答案

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

这篇关于在python中重新组合这两个列表的聪明方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆