python - Rank an array of tuples -
i have array
array = [('item1',90),('item2',76),('item3',83),('item4',90)]
and i'd rank like
ranking = [(0,'item1'),(0,'item4'),(2,'item3'),(3,'item2')] // (1)
this i've done far, using ranking:
from ranking import * >>> array.sort(key=lambda x: x[1],reverse = true) >>> print [i in list(ranking([i[1] in array]))]
this get:
[(0, 90), (0, 90), (2, 83), (3, 76)] // (2)
can help me (2) (1)? don't need utilize ranking
. take other solution.
array = [('item1',90),('item2',76),('item3',83),('item4',90)] srt = sorted(array,key=lambda x: x[1], reverse=true) rankings = [] rank = 0 itertools import groupby k,v in groupby(srt,lambda x: x[1]): # grouping score grp = [(rank,tup[0]) tup in v] # item tup[0] , set in tuple rank rankings += grp rank += len(grp) # increment rank next grouping print(rankings) [(0, 'item1'), (0, 'item4'), (2, 'item3'), (3, 'item2')]
python ranking
No comments:
Post a Comment