| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1139 人关注过本帖
标题:关于python中字典列表嵌套使用的问题
只看楼主 加入收藏
dahere
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-2-19
结帖率:0
收藏
已结贴  问题点数:5 回复次数:1 
关于python中字典列表嵌套使用的问题
我写了下面一段代码:
一、建立了一个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)])
搜索更多相关主题的帖子: 元素 score 列表 std print 
2020-02-19 10:58
时光流逝
Rank: 4
来 自:北京
等 级:业余侠客
威 望:8
帖 子:92
专家分:297
注 册:2019-11-16
收藏
得分:5 
你用copy()方法试一下
例:
dict1={'Name':'Tom','Sex':'Male'}
dict2=dict1.copy()

2020-02-23 17:47
快速回复:关于python中字典列表嵌套使用的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015787 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved