Home > other >  Help: questions about python dictionary use nested list
Help: questions about python dictionary use nested list

Time:09-28

I wrote a piece of code below:

 
Students=[] # define a students list
Stu_id=1
Score=100
For num in range (1, 7) : # list
element is a dictionary structureNew_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 ())

"' definition teached_students list, is the student list is a subset of the '"'
Teached_students=students/- 4:

Print (" \ n -- -- -- -- -- -- -- -- print teached_students information -- -- -- -- -- -- -- ")
For STD in teached_students:
Print (STD) items ())

"' modify students [3] and delete students [1] ' '
Students [3] [' score ']=59
Del students [1]
Students. Append ({' stu_id: 99, 'score: 120})

Print (" \ n -- -- -- -- -- -- -- -- -- -- After the modify students [3] and delelet students [1] -- -- -- -- -- -- -- ")
Print (" \ n -- -- -- -- -- -- -- -- print teached_students information -- -- -- -- -- -- -- ")
Print (" \ t -- -- -- -- -- -- -- -- -- according to does the \ "teached_students [3] " changed, \ n + "
"\ t -- -- -- -- -- -- -- -- -- but " teached_students [1] \ "haven 't had been removed? -- -- -- -- -- -- -- -- --
")For STD in teached_students:
Print (STD) items ())
Print (" \ n -- -- -- -- -- -- -- -- print students information -- -- -- -- -- -- -- ")
For STD in students:
Print (STD) items ())





One, set up a list of students, is list element contains two key dictionary,
Two, established a teached_students list, he is a list of students an interception, namely he subset,
Three, modify the list of students an element, delete an element, (selection of the two elements are included in the list teached_students)
Four, found the problem: to modify the list of student element value, why teached_students elements in the list of value also changed?
Does this mean for copy dictionary list elements, and no real allocates memory space, but only made an index?
But if so, why to delete the elements in the list of students, the teached_students list elements are not deleted at the same time?

The results are as follows:
-- -- -- -- -- -- -- -- print students information -- -- -- -- -- -- --
Dict_items ([(' stu_id ', 1), (100) 'score'])
Dict_items ([(' stu_id, 2), (98) 'score'])
Dict_items ([(' stu_id, 4), (96) 'score'])
Dict_items ([(' stu_id, 7), (94) 'score']) # modified score value of the element
Dict_items ([(' stu_id, 11), (92) 'score'])
Dict_items ([(' stu_id, 16), (90) 'score']) # to delete this element

-- -- -- -- -- -- -- -- print teached_students information -- -- -- -- -- -- --
Dict_items ([(' stu_id, 4), (96) 'score'])
Dict_items ([(' stu_id, 7), (94) 'score'])
Dict_items ([(' stu_id, 11), (92) 'score'])
Dict_items ([(' stu_id, 16), (90) 'score'])

-- -- -- -- -- -- -- -- -- -- After the modify students [3] and delelet students [1] -- -- -- -- -- -- --

-- -- -- -- -- -- -- -- print teached_students information -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- according to does the "teached_students [3]" changed,
-- -- -- -- -- -- -- -- -- but "teached_students [1]" haven 't had been removed? -- -- -- -- -- -- -- -- --
Dict_items ([(' stu_id, 4), (96) 'score'])
Dict_items ([(' stu_id, 7), (' score '59)]) # why this element score values changed?
Dict_items ([(' stu_id, 11), (92) 'score'])
Dict_items ([(' stu_id, 16), (90) 'score']) # why didn't the element to be deleted? !!!!!!!!!

-- -- -- -- -- -- -- -- print students information -- -- -- -- -- -- --
Dict_items ([(' stu_id ', 1), (100) 'score'])
Dict_items ([(' stu_id, 2), (98) 'score'])
Dict_items ([(' stu_id, 4), (96) 'score'])
Dict_items ([(' stu_id, 7), (' score '59)])
Dict_items ([(' stu_id, 11), (92) 'score'])
Dict_items ([(' stu_id, 99), (120) 'score'])

CodePudding user response:

I've tried, if used separately list, won't appear this problem; But if you use a dictionary list, problems arise, ask ace to help solve,,,
  • Related