list = ('Bob', 11, 333, 453.3, 'Ted', 15, 999, 345.5, 'Jeff', 22, 122, 434.7)
嗨,我正在尝试根据上述信息构建前 10 名列表。
第一个值:进球,第二个值:助攻第三个值:上场时间
在这种情况下,我希望能够找到总目标最高的用户,我希望得到以下结果:
1)杰夫:22 2)泰德:15 3)鲍勃:11
你应该在这里使用字典:
>>> lis = ('Bob', 11, 333, 453.3, 'Ted', 15, 999, 345.5, 'Jeff', 22, 122, 434.7)
>>> dic = { lis[i]: list(lis[i+1:i+4]) for i in xrange(0,len(lis),4)}
>>> dic
{'Bob': [11, 333, 453.3],
'Ted': [15, 999, 345.5],
'Jeff': [22, 122, 434.7]}
>>> sorted(dic,key = dic.get,reverse = True)
['Jeff', 'Ted', 'Bob']
# To get keys as well as values use dict.items()
>>> for k,v in sorted(dic.items(), key =lambda x:x[1] , reverse = True):
print k,v[0]
...
Jeff 22
Ted 15
Bob 11
>>> from operator import itemgetter
>>> data = ('Bob', 11, 333, 453.3, 'Ted', 15, 999, 345.5, 'Jeff', 22, 122, 434.7)
>>> for x in sorted(zip(*[iter(data)]*4), key=itemgetter(1), reverse=True):
print x[0], x[1]
Jeff 22
Ted 15
Bob 11