注册 登录
编程论坛 Python论坛

关于python中字典列表嵌套使用的问题

dahere 发布于 2020-02-19 10:58, 1231 次点击
我写了下面一段代码:
一、建立了一个students列表,列表元素的学生信息为包含2个键的字典。
二、建立了一个teached_students列表,他是对students列表的一个截取。
三、修改学生列表中一个元素,删除一个元素。(选取的这两个元素都包含在teached_students列表中)
四、发现问题:修改student列表中元素value时,为什么teached_students列表中的元素value也变了?
          难道这是说对于字典列表元素的复制,并没有真正的分配内存空间,而是仅仅做了一个索引?
         但若是如此,为什么删除students列表中的元素时,teached_students列表中的元素没有同时被删除呢?

students = []
stu_id = 1
score = 100
for num in range(1,12):
    new_student = {
        'stu_id':stu_id,
        'score':score
        }
    students.append(new_student)
    stu_id = stu_id + num
    score = 100 - num* 2
print("--------print students information-------")
for std in students:
    print(std.items())   
   
'''define teached_students list and initial it'''
teached_students = []
teached_students = students[-5:]

print("\n--------print teached_students information-------")
for std in teached_students:
    print(std.items())
   
'''modify students[-3] and delelet students[-1]'''
students[-3]['score'] = 59
del students[-1]

print("\n----------modify students[-3] and delelet students[-1]-------")
print("-----------why does the \"teached_students[-3]\" changed but \"teached_students[-1]\" haven't been removed?---------")
for std in teached_students:
    print(std.items())
print("\n--------print students information-------")
for std in students:
    print(std.items())   

运行结果如下:
--------print students information-------
dict_items([('stu_id', 1), ('score', 100)])
dict_items([('stu_id', 2), ('score', 98)])
dict_items([('stu_id', 4), ('score', 96)])
dict_items([('stu_id', 7), ('score', 94)])
dict_items([('stu_id', 11), ('score', 92)])
dict_items([('stu_id', 16), ('score', 90)])
dict_items([('stu_id', 22), ('score', 88)])
dict_items([('stu_id', 29), ('score', 86)])
dict_items([('stu_id', 37), ('score', 84)])                                 //修改此元素的score值
dict_items([('stu_id', 46), ('score', 82)])
dict_items([('stu_id', 56), ('score', 80)])                            //删除此元素

--------print teached_students information-------
dict_items([('stu_id', 22), ('score', 88)])
dict_items([('stu_id', 29), ('score', 86)])
dict_items([('stu_id', 37), ('score', 84)])
dict_items([('stu_id', 46), ('score', 82)])
dict_items([('stu_id', 56), ('score', 80)])

----------modify students[-3] and delelet students[-1]-------
-----------why does the teached_students list changed?---------
dict_items([('stu_id', 22), ('score', 88)])
dict_items([('stu_id', 29), ('score', 86)])
dict_items([('stu_id', 37), ('score', 59)])                       //为什么此元素的score值变了?
dict_items([('stu_id', 46), ('score', 82)])
dict_items([('stu_id', 56), ('score', 80)])                     //为什么此元素没被删除?!!!

--------print students information-------
dict_items([('stu_id', 1), ('score', 100)])
dict_items([('stu_id', 2), ('score', 98)])
dict_items([('stu_id', 4), ('score', 96)])
dict_items([('stu_id', 7), ('score', 94)])
dict_items([('stu_id', 11), ('score', 92)])
dict_items([('stu_id', 16), ('score', 90)])
dict_items([('stu_id', 22), ('score', 88)])
dict_items([('stu_id', 29), ('score', 86)])
dict_items([('stu_id', 37), ('score', 59)])
dict_items([('stu_id', 46), ('score', 82)])
1 回复
#2
时光流逝2020-02-23 17:47
你用copy()方法试一下
例:

dict1={'Name':'Tom','Sex':'Male'}
dict2=dict1.copy()

1