(I apologize that previous versions of this question displayed the wrong function that I need to fix, this has been remedied and I hope the question makes a little more sense now.)
I have a list of objects with scores, and I'm attempting to assign rank to them based on those scores. Below is basically how I output my data.
sorted_scores = [
('Apolo Ohno', 0),
('Shanie Davis', -1),
('Bodie Miller', -2),
('Lindsay Vohn', -3),
('Shawn White', -3),
('Bryan Veloso', -4)
]
I have a tie. Now, the function that assigns positions to the objects above right now is a simple for loop that just assigns the value of i
as the object's final position.
positions = {}
i = 1
for key, value in sorted_list:
# Since in my codebase the strings are IDs, I use the key to fetch the object.
if value is not None:
positions[key] = i
i += 1
So that'll obviously return:
positions = {
'Apolo Ohno': 1,
'Shanie Davis': 2,
'Bodie Miller': 3,
'Lindsay Vohn': 4,
'Shawn White': 5,
'Bryan Veloso': 6
}
Hopefully that makes some sense. The meat of the question is that loop. What makes more sense is if it returns them like so:
positions = {
'Apolo Ohno': 1,
'Shanie Davis': 2,
'Bodie Miller': 3,
'Lindsay Vohn': 4, # Same value.
'Shawn White': 4, # Same value.
'Bryan Veloso': 6
}
How would I edit the function above to do that, keeping in mind that I could have any number of ties at any given time depending on how many of my members ranked said object? The highest rank should be 1, so it can be displayed as such: <rank>/<total # of people>
Thanks in advance. :)
>>> sorted_scores = [
... ('Apolo Ohno', 0),
... ('Shanie Davis', -1),
... ('Bodie Miller', -2),
... ('Lindsay Vohn', -3),
... ('Shawn White', -3),
... ('Bryan Veloso',-4)
... ]
>>>
>>> res = {}
>>> prev = None
>>> for i,(k,v) in enumerate(sorted_scores):
... if v!=prev:
... place,prev = i+1,v
... res[k] = place
...
>>> print res
{'Apolo Ohno': 1, 'Bryan Veloso': 6, 'Shanie Davis': 2, 'Lindsay Vohn': 4, 'Bodie Miller': 3, 'Shawn White': 4}
Remember that dicts are unordered, so to iterate in order of place, you need to do this
>>> from operator import itemgetter
>>> print sorted(res.items(),key=itemgetter(1))
[('Apolo Ohno', 1), ('Shanie Davis', 2), ('Bodie Miller', 3), ('Lindsay Vohn', 4), ('Shawn White', 4), ('Bryan Veloso', 6)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With