Home > Net >  Saving in list as value in dicionary
Saving in list as value in dicionary

Time:06-16

I have a dictionary in pyhton with integers as keys and lists as values. The lists are filled with nine empty strings respectively. When printed the dict (session.feedback) looks like this:

{1: ['', '', '', '', '', '', '', '', ''], 2: ['', '', '', '', '', '', '', '', ''], 3: ['', '', '', '', '', '', '', '', ''], 4: ['', '', '', '', '', '', '', '', ''], 5: ['', '', '', '', '', '', '', '', ''],
 6: ['', '', '', '', '', '', '', '', ''], 7: ['', '', '', '', '', '', '', '', ''], 8: ['', '', '', '', '', '', '', '', ''], 9: ['', '', '', '', '', '', '', '', ''], 10: ['', '', '', '', '', '', '', '', '']
}

I'm now trying to add a certain string to one of those lists only:

worker = random.randint(1, 9)
session.feedback[player.groupno][worker] = player.feedback

The group number (groupno) is a previously assigned integer. player.feedback is a string entered by the participant of an experiment.

However, when printing session.feedback after the code has run and I entered some text, this is the result:

{1: ['', '', '', '', '', '', '', '', 'test'], 2: ['', '', '', '', '', '', '', '', 'test'], 3: ['', '', '', '', '', '', '', '', 'test'], 4: ['', '', '', '', '', '', '', '', 'test'], 5: ['', '', '', '', '', 
'', '', '', 'test'], 6: ['', '', '', '', '', '', '', '', 'test'], 7: ['', '', '', '', '', '', '', '', 'test'], 8: ['', '', '', '', '', '', '', '', 'test'], 9: ['', '', '', '', '', '', '', '', 'test'], 10: 
['', '', '', '', '', '', '', '', 'test']}

Why does it save the string in every list? Does it simply ignore the key (groupno)? How can I fix this?

Thanks in advance!

CodePudding user response:

perhaps printing out player.groupno will help to understand why it’s changing every list’s n string. I don’t know why it’s doing that but my first thought is that player.groupno is iterating through group numbers. Make sure that player.groupno is a single value and isn’t iterating through all group numbers.

Otherwise, randint does include the end number, in the possible results. So right now, worker could be 9. But each list in session.feedback has an index that starts at 0 and ends at 8. so maybe this could help:

worker = random.randint(0,8)

session.feedback[player.groupno][worker] = player.feedback

CodePudding user response:

There was a problem with how I initialised the dictionary.By using "emptylist", the lists for the different keys were seen as the same and changing one changed the others (or rather the same one) as well:

    session.feedback = {}
    gid = 1
    emptylist = ['', '', '', '', '', '', '', '', '']
    while gid < (11):
        session.feedback[gid] = emptylist
        gid = gid   1

I tried initialising it the following way and it worked as intended:

    session.feedback = {}
    gid = 1
    while gid < (11):
        session.feedback[gid] = ['', '', '', '', '', '', '', '', '']
        gid = gid   1

Thanks for your help anyway! (Especially to depperm who made me look at that again)

  • Related