Python Forum
In this dictionary all the values end up the same. How?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In this dictionary all the values end up the same. How?
#1
What I'm doing is making a big excel with all the students homework answers in each row. The first 2 columns are name and student number.

My goal is to make a MySQL table with all the students' answers, so they can click a button and see their answers and the correct answers. Eventually, I will do everything on the webhost in MySQL and stop downloading files.

A typical file is a text file with a name composed of:

Quote:studentnumber_COURSEweek_microtime epochtime like this: 1925010101_19BEwW3_0.74946700 1600771500

The times are so each submission will have a different name, in case a student submits more than 1 time.
The content is like this, just longer:

Quote:Studentnr = 1925010101
B
B
C
A
These are written to my webpage by PHP, I download them and mark them.

Yesterday, I did this below. Can anyone see anything wrong with it?

When I checked the dictionary studentAnswers, the keys were all different, but the values were all the same.

The value was always a list of the answers in the last file in the list of files weekXfiles. The student number was 1925010310. FOR EVERY KEY!

I can't understand what is wrong, apart from thinking there is a bug in my Python installation, or in Idle.

weekXfiles is a list of all files from 1 particular week, like 19BEwW3

studentAnswers = {}
studentAnswersList = []

for hw in weekXfiles:
    studentAnswersList.clear()    
    data = open(pathHW + hw, 'r')
    dataList = data.readlines()    
    data.close()
    name = hw[21:]    
    for i in range(0, len(dataList)):
        studentAnswer = dataList[i].replace('\n', '')
        studentAnswersList.append(studentAnswer)
    studentAnswers[name] = studentAnswersList    
I do the same kind of thing often, for example, for marking: use the student number as key and the score as value, make a dictionary, then read it into excel. Never had this problem.

In the end I avoided using a dictionary and just made a list and wrote it directly to excel. That worked.
Reply
#2
Each time through the loop you're reusing the same list. When you .clear() the list, you're clearing the data from all the students (because they share the same object).

You want to use a unique object for each student. Instead of creating outside the loop and reusing it, create it new each time.

studentAnswers = {}
 
for hw in weekXfiles:
    studentAnswersList = []  # New object for this student.
    ...  
Pedroski55 likes this post
Reply
#3
Thank you, you are right, I just tried it!

I thought studentAnswersList.clear() did the same job as studentAnswersList = []

I don't understand it, but I am the wiser, thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 282 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 863 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,393 Apr-12-2023, 08:10 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,747 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,582 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,017 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,994 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,487 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Counting the values ​​in the dictionary Inkanus 7 3,646 Oct-26-2020, 01:28 PM
Last Post: Inkanus
  How to make a list of values from a dictionary list? faryad13 2 2,072 Sep-03-2020, 03:45 PM
Last Post: faryad13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020