尝试在 python 中创建字典列表时出现意外列表 [英] Unexpected list when trying to create a list of dictionaries in python

查看:26
本文介绍了尝试在 python 中创建字典列表时出现意外列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个字典列表.预期的输出是对于 5 个学生,列表中应该有 5 个字典项,键为 Name 和 Marks(在列表中).我得到的输出是迭代中最后一个学生的信息重​​复 5 次.请让我知道我在哪里犯了错误.

随机导入名称列表=[]name_list = ['A','B','C','D','E']std_dict=dict()std_dict_list = []对于 name_list 中的名称:标记列表 = []对于范围内的 i (1,11):mark_list.append(random.randint(0,100))stud_dict['Name']=namestud_dict['Marks']=mark_liststud_dict_list.append(stud_dict)打印(stud_dict_list)输出:[{'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks':[91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name':'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}]

解决方案

这已经被问过很多次了,但我没有时间去搜索一个确切的 dup ATM,所以:

Python 从不复制东西,除非明确要求.在这里,您一遍又一遍地将相同 dict 添加到您的列表中:

<预><代码>>>>随机导入>>>名称列表=[]>>>name_list = ['A','B','C','D','E']>>>std_dict=dict()>>>std_dict_list = []>>>对于 name_list 中的名称:... 标记列表 = []...对于范围内的我(1,11):... mark_list.append(random.randint(0,100))...stud_dict['Name']=name...stud_dict['Marks']=mark_list...stud_dict_list.append(stud_dict)...>>>梭哈字典{'姓名':'E','标记':[94, 51, 79, 82, 84, 76, 92, 96, 7, 52]}>>>id(stud_dict)139663685096344>>>[stud_dict_list 中项目的 id(item)][139663685096344、139663685096344、139663685096344、139663685096344、1396663685096344]>>>

由于是同一个字典,字典的每次更新当然都会反映在列表中.解决方案很简单,每次都创建一个新的字典:

随机导入name_list = ['A','B','C','D','E']std_dict_list = []对于 name_list 中的名称:标记列表 = []对于范围内的 i (1,11):mark_list.append(random.randint(0,100))stud_dict = {'Name':名称,'标记':mark_list}stud_dict_list.append(stud_dict)

您还想阅读本文以了解有关 Python 变量的更多信息...

I am trying to create a list of dictionary. The expected output is that for 5 Students, it should have 5 dictionary items in the list with keys being Name and Marks (in a list). The output that I get is having the information repeated 5 times for the last student in iteration. Please let me know where am I making an error.

import random
name_list=[]
name_list = ['A','B','C','D','E']
stud_dict=dict()
stud_dict_list = []
for name in name_list:
    mark_list = []
    for i in range(1,11):
        mark_list.append(random.randint(0,100))
    stud_dict['Name']=name
    stud_dict['Marks']=mark_list
    stud_dict_list.append(stud_dict)
print(stud_dict_list) 

output:
[{'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}]

解决方案

This has been asked many times but I don't have time to search for an exact dup ATM, so:

Python never copies things unless explicitely asked for. Here, you're adding the same dict to your list over and over again:

>>> import random
>>> name_list=[]
>>> name_list = ['A','B','C','D','E']
>>> stud_dict=dict()
>>> stud_dict_list = []
>>> for name in name_list:
...     mark_list = []
...     for i in range(1,11):
...         mark_list.append(random.randint(0,100))
...     stud_dict['Name']=name
...     stud_dict['Marks']=mark_list
...     stud_dict_list.append(stud_dict)
... 
>>> stud_dict
{'Name': 'E', 'Marks': [94, 51, 79, 82, 84, 76, 92, 96, 7, 52]}
>>> id(stud_dict)
139663685096344
>>> [id(item) for item in stud_dict_list]
[139663685096344, 139663685096344, 139663685096344, 139663685096344, 139663685096344]
>>> 

Since it's the very same dict, each update of the dict will of course be reflected in the list. And the solution is quite simply to create a new dict each time:

import random
name_list = ['A','B','C','D','E']
stud_dict_list = []
for name in name_list:
    mark_list = []
    for i in range(1,11):
        mark_list.append(random.randint(0,100))
    stud_dict = {'Name': name, 'Marks': mark_list}
    stud_dict_list.append(stud_dict)

You also want to read this for more on Python's variables...

这篇关于尝试在 python 中创建字典列表时出现意外列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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