(python3.7.x)
{id:score}
の入った配列 id_score_list
の id:2
が score 降順で順位が何番めなのかを取得したいメモ
id_score_list = [{3: 180}, {5: 150}, {10: 150}, {1: 120}, {8: 120}, {9: 120}, {2: 110}, {7: 90}, {6: 70}, {4: 60}] target_id = 2 ids = [list(d.items())[0][0] for d in id_score_list] scores = [list(d.items())[0][1] for d in id_score_list] target_index = ids.index(target_id) print('total_count', len(scores)) print('target_id', target_id) ranking = len(scores[:target_index]) + 1 print('ranking', ranking) before_duplicate_ranking_count = len(set(scores[:target_index])) print('before_duplicate_ranking_count', before_duplicate_ranking_count) fix_ranking = before_duplicate_ranking_count + 1 print('fix_ranking', fix_ranking) # total_count 10 # target_id 2 # ranking 7 # before_duplicate_ranking_count 3 # fix_ranking 4
id:2
のランキング7位だったが、同順位を考慮すると4位になった。