python列表嵌套 分配学生_学生信息中列表嵌套使用的问题--python代码工作笔记(1)...

一、建立了一个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)])

-------------------好,今天可以下班了,嘿嘿--------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值