1

I have information from a file and want to read it in the form dictionary of dictionary.

Informations are: ID, name and exercise marks.

this is the simple input:

s11111 Ahmed 20 18 15 20 20 18 s55555 Suaad 20 20 18 18 15 16 16 19 s33333 Jihad 15 14 16 s22222 Jassim 20 19 20 18 17 18 20 20 19 19 s77777 Badria 20 18 20 18 20 18 20 18 19

the main dic consist of student id and the value will be the second dic with key of name and value = list of score.

I used this code but every time I rewrite all the second dictionary information for each ID

infile = open("score.txt","r")
line = infile.readline()
stdict = {}
dict2 = {}
while line != "":
      line = line.rstrip()
      part = line.split(" ")
      studentId = part[0]
      studentName = part[1]
      studentgrade = part[2:9]
      studentgrade = list(map(int, studentgrade))
      stdict[studentId] = dict2
      dict2[studentName] = studentgrade
      line = infile.readline()
print(stdict)

the output become like :

can anyone correct my code?

and i wont to know how i can use recursive sort to sort it by values of score if i include this dictionaries into linked list !

Aya
  • 33
  • 1
  • 6
  • `stdict[studentId] = dict2` assigns the same `dict2` reference to all keys, sharing that dictionary. You need to create a *new, empty* dictionary at that point, or create a copy. See the duplicate for the latter. – Martijn Pieters May 06 '18 at 13:11
  • where i should create a new dictionary? I do not understand – Aya May 06 '18 at 13:16

0 Answers0