简体   繁体   English

如何将列表作为新项目加入列表字典-python?

[英]How do you join a list to a dictionary of lists as a new item - python?

Maybe a simple question: 也许是一个简单的问题:

In python I have a list of dictionaries and I want to add a list as new item in every dictionary in the list? 在python中,我有一个字典列表,我想在列表中的每个字典中添加一个列表作为新项?

For example I have the list of dictionaries: 例如,我有字典列表:

list_dict =[{'id':1, 'text':'John'},
            {'id':2, 'text':'Amy'},
            {'id':3, 'text':'Ron'}]

And a list: 并列出:

list_age = [23, 54, 41]

How could I then add the list to produce the list of dictionaries: 然后,如何添加列表以生成字典列表:

list_dict =[{'id':1, 'text':'John', 'age':23},
            {'id':2, 'text':'Amy', 'age':54},
            {'id':3, 'text':'Ron', 'age':41}]

I am unsure of the correct code to use here? 我不确定在这里使用正确的代码吗?

Use zip , to iterate over the matching pairs and update the dicts: 使用zip ,遍历匹配对并更新字典:

>>> for d, a in zip(list_dict, list_age):
...     d["age"] = a
... 
>>> list_dict
[{'id': 1, 'text': 'John', 'age': 23}, {'id': 2, 'text': 'Amy', 'age': 54}, {'id': 3, 'text': 'Ron', 'age': 41}]

Try this loop if list_age and list_dict are of same length : 如果list_agelist_dict的长度相同,请尝试以下循环:

for i, j in zip(list_dict, list_age):
  i['age']=j

OUTPUT : 输出

[{'id': 1, 'text': 'John', 'age': 23}, {'id': 2, 'text': 'Amy', 'age': 54}, {'id': 3, 'text': 'Ron', 'age': 41}]

Something like this could work 这样的事情可能会起作用

for index, item in enumerate(list_age):
  list_dict[index]['age'] = item

Edit: As @Netwave mentioned, you should make sure that len(list_age) is not greater than len(list_dict) . 编辑:如@Netwave所述,您应确保len(list_age)不大于len(list_dict)

Add the list to produce the list of dictionaries: 添加列表以产生字典列表:

for a, b in zip(list_dict, list_englishmark):
    a["englishmark"] = b

print(list_dict)

Output: 输出:

[{'id': 1, 'name': 'mari', 'englishmark': 80}, {'id': 2, 'name': 'Arun', 'englishmark': 54}, {'id': 3, 'name': 'ram', 'englishmark':75}] [{'id':1,'name':'mari','englishmark':80},{'id':2,'name':'Arun','englishmark':54},{'id': 3,'name':'ram','englishmark':75}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在字典列表中拆分字典字符串值以添加新的字典项? - How do you split a dictionary string value in a list of dictionaries to add a new dictionary item? 如何根据 python 中的条件删除列表列表中的项目? - How do you delete an item in a lists of lists based on a condition in python? 在 Python 中,如果您有两个共享多个元素的列表,您如何创建具有匹配项的新列表? - In Python, if you have two lists that share a number of elements, how do you create a new list with the matches? 如何获取列表列表中的值/用字典值替换它? - How do you get at a value in a list of lists /replace it with a dictionary value? 如何使用Python中的嵌套列表创建字典? - How do you create a dictionary from nested lists in Python? 如何使用不同大小的列表创建 Python 字典? - How do you create a python dictionary using lists of different size? Python:如何在python的列表列表中添加列表? - Python: How do you add a list to a list of lists in python? 如何将字典列表转换为Python中的列表字典? - How do I convert a list of dictionaries to a dictionary of lists in Python? Python:列表中的值在列表字典中出现多少次? - Python: how many times do the values in a list appear in a dictionary of lists? 如何将列表列表转换为以下格式的 python 字典? - How do I convert a list of lists to a python dictionary of the following format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM