也许一个简单的问题:
在python中,我有一个字典列表,我想在列表中的每个字典中添加一个列表作为新项?你知道吗
例如,我有字典列表:
list_dict =[{'id':1, 'text':'John'},
{'id':2, 'text':'Amy'},
{'id':3, 'text':'Ron'}]
以及一份清单:
list_age = [23, 54, 41]
然后如何添加列表以生成词典列表:
list_dict =[{'id':1, 'text':'John', 'age':23},
{'id':2, 'text':'Amy', 'age':54},
{'id':3, 'text':'Ron', 'age':41}]
我不确定这里使用的代码是否正确?你知道吗
像这样的事情可能有用
编辑: 正如@Netwave提到的,您应该确保
len(list_age)
不大于len(list_dict)
。你知道吗如果
list_age
和list_dict
长度相同,请尝试此循环:输出:
使用
zip
迭代匹配对并更新dict:相关问题 更多 >
编程相关推荐