0

I am trying to write a simple function that gives the result of a runoff vote.

I start with a nested list with names of candidates, and I would like to group them by the first element and put that into a dictionary (where the first element is the key and a nested list of all the lists with this first element is the value)

def runoff_rec(xxx):
    print(xxx)


    sortedvotes = groupby(xxx, key=lambda x: x[0])
    votesdict = {}
    for key, value in sortedvotes:
        votesdict[key] = list(value)


    print(votesdict)

at the first print, the nested list looks like this:

[['Johan Liebert', 'Daisuke Aramaki', 'Lex Luthor', 'Gihren Zabi'], 
['Daisuke Aramaki', 'Gihren Zabi', 'Johan Liebert', 'Lex Luthor'], 
['Daisuke Aramaki', 'Lex Luthor', 'Gihren Zabi', 'Johan Liebert'], 
['Johan Liebert', 'Gihren Zabi', 'Lex Luthor', 'Daisuke Aramaki'], 
['Lex Luthor', 'Johan Liebert', 'Daisuke Aramaki', 'Gihren Zabi'], 
['Gihren Zabi', 'Daisuke Aramaki', 'Johan Liebert', 'Lex Luthor']]

but when I print the dictionary it looks like this:

{'Johan Liebert': [['Johan Liebert', 'Gihren Zabi', 'Lex Luthor', 'Daisuke Aramaki']], 
'Daisuke Aramaki': [['Daisuke Aramaki', 'Gihren Zabi', 'Johan Liebert', 'Lex Luthor'], ['Daisuke Aramaki', 'Lex Luthor', 'Gihren Zabi', 'Johan Liebert']],
 'Lex Luthor': [['Lex Luthor', 'Johan Liebert', 'Daisuke Aramaki', 'Gihren Zabi']], 
'Gihren Zabi': [['Gihren Zabi', 'Daisuke Aramaki', 'Johan Liebert', 'Lex Luthor']]}

One of the values from the list (the first one) has disappeared. Any idea why that might happen?

Thank you in advance, have a beautiful day

  • A dict cannot have duplicate keys. The second assignment for the key `Daisuke Aramaki` has overwritten the previous assignment. The same for `John Liebert`. – j1-lee Jul 07 '22 at 03:07
  • I know, but does that apply here? The way I intended it to work is that the `groupby`sorts all the lists into separate elements, each with different key (for example, all the ones starting with `John Liebert` would be in one entry in the groupby object). if they're all in a nested list under a single key, they should not overwrite. `Daisuke Aramaki` seems to work fine, creating a dict value with 2 lists, it's just the `John Liebert` that doesn't work – Janek Pawlak Jul 07 '22 at 03:54
  • Oh my bad! I didn't see the second sublist for `Daisuke Aramaki`. But anyway, `Johan Liebert` still might be due to the duplicate key issue; you didn't sort the input list before `groupby`, so that `groupby` treats `Johan Liebert` in two separate cases. Note that `groupby` only groups adjacent entities. This might be an explanation why the answer below works. – j1-lee Jul 07 '22 at 03:55
  • oh right, that would make sense. thank you! – Janek Pawlak Jul 07 '22 at 05:31

1 Answers1

1

i guess u want this

def runoff_rec(xxx):
    print(xxx)

    xxx.sort(key=lambda x: x[0])
    sortedvotes = groupby(xxx, key=lambda x: x[0])
    votesdict = {}
    for key, value in sortedvotes:
        votesdict[key] = list(value)


    print(votesdict)

groupby comment

""" make an iterator that returns consecutive keys and groups from the iterable iterable Elements to divide into groups according to the key function. key A function for computing the group category for each element. If the key function is not specified or is None, the element itself is used for grouping. """

consecutive is importan

  • Not sure how, but it worked. Thank you, I'll still be looking for an explanation but the code you provided works really well. – Janek Pawlak Jul 07 '22 at 03:48
  • update: the solution explained in the comments, by j1-lee, thank you both for contributing – Janek Pawlak Jul 07 '22 at 05:31
  • """ make an iterator that returns consecutive keys and groups from the iterable iterable Elements to divide into groups according to the key function. key A function for computing the group category for each element. If the key function is not specified or is None, the element itself is used for grouping. """ consecutive is important – 495088732qqcom Jul 07 '22 at 05:34
  • @495088732qqcom nice answer! However, remember that others might stumble onto your answer at a later date when they have a similar issue, consider editing your answer to include the explanation rather than leaving it as a comment. Cheers – G. Anderson Jul 07 '22 at 15:40