Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4778
  • 博文数量: 1
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-25 16:29
个人简介

Study is endless!

文章分类

全部博文(1)

文章存档

2020年(1)

我的朋友
最近访客

分类: Python/Ruby

2020-02-19 11:20:52

我写了下面一段代码:
一、建立了一个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)])
阅读(825) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~