2 回答
TA贡献1810条经验 获得超4个赞
您可以使用 ,即:pandas
df = pd.DataFrame(data)
df['rank'] = df['value'].rank(ascending=True, method="min").astype(int)
df = df.sort_values(by=['rank']).reset_index(drop=True)
for i in range(1, len(df)):
last, curr = df.loc[i-1, 'rank'], df.loc[i, 'rank']
df.loc[i, 'rank'] = last + 1 if last < curr else last
print(df)
playerId value rank
0 2 196763.8 1
1 3 196763.8 1
2 44 196764.0 2
TA贡献1831条经验 获得超9个赞
您需要按以下条件对列表进行排序,并查找某些列表是否与其他列表具有相同的分数,并将它们合并为一个。valueplayer_id
这应该可以解决问题:
from collections import defaultdict
new_dict = defaultdict(list)
for x in data:
new_dict[x['value']].append(x['playerId'])
rank_list = [[k,v] for k,v in new_dict.items()]
# Set reverse=False if lower score is considered higher rank
rank_list.sort(key=lambda k: k[0], reverse=True)
def get_rank(player_id):
for i in range(len(rank_list)):
if player_id in rank_list[i][1]:
return i+1 # list is 0 indexed thus +1
print('Rank is %s for player id %s' % (get_rank(44), 44))
> Rank is 1 for player id 44
print('Rank is %s for player id %s' % (get_rank(2), 2))
> Rank is 2 for player id 2
print('Rank is %s for player id %s' % (get_rank(3), 3))
> Rank is 2 for player id 3
添加回答
举报