一、建立了一个students列表,列表元素的学生信息为包含2个键的字典。
二、建立了一个teached_students列表,他是对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)])
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)])
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)])
-------------------好,今天可以下班了,嘿嘿--------------------------