从列表中的分数元组分配排名
Assign ranking from tuples of scores in list
我在一个列表中有三个元组,如果我是正确的话,我正在尝试 return 给定游戏的三个最佳玩家的排名。考虑以下因素:
players_score = [ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ]
正在打印第一个游戏的结果 returns 以下比分:
[('Joe', 100), ('Bob', 90), ('May', 80), ('Anna', 95)]
但是,我想让程序打印这个:
[ ('Joe', 1), ('Anna', 2), ('Bob', 3) ]
由于乔有 100 分,安娜有 95 分,鲍勃有 90 分。
我正在考虑使用 enumerate()
,但我认为首先将元组转换为字典可能会更好,我在代码的最后部分尝试过,但运气不佳。
我开始尝试以下方法:
#prints the corresponding score with the player.
game = int(input("Please enter the number of a game: "))
game_score = [ (p[0], p[game]) for p in players_score]
print(game_score)
#prints a list of results ordered from high to low.
score_list = []
for p in players_score:
score_list.append(p[game])
score_list.sort(reverse=True)
print(score_list)
#prints only the name and the result of the first game
list = [ (name, first) for name, first, second, third, fourth, sixth, seventh, eighth in players_score ]
print(list)
#trying to get a convert from a tuple to a dict.
for name, first, second, third, forth, sixth, seventh, eighth in players_score:
dict(name, first)
print(dict)
你有这个:
a = [('Joe', 100), ('Bob', 90), ('May', 80), ('Anna', 95)]
尝试使用每个元组的第二个参数对其进行排序:
a = sorted(a, key=lambda x:x[1], reverse=True)
然后:
result = [(v[0], i+1) for i,v in enumerate(a)][0:3]
输出将是:
[('Joe', 1), ('Anna', 2), ('Bob', 3)]
此外,对于第一个答案,您可以将玩家得分转换为字典,以方便之后的操作:
例如:
players_score = [ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ]
# convert player's scores to dictionary
games = {}
for (player_name, *scores) in players_score:
for game_no, score in enumerate(scores, 1):
games.setdefault(game_no, {}).setdefault(player_name, {})
games[game_no][player_name] = score
# the dictionary "games" will look like:
# {1: {'Anna': 95, 'Bob': 90, 'Joe': 100, 'May': 80},
# 2: {'Anna': 32, 'Bob': 38, 'Joe': 34, 'May': 36},
# 3: {'Anna': 36, 'Bob': 4, 'Joe': 38, 'May': 40},
# 4: {'Anna': 92, 'Bob': 100, 'Joe': 90, 'May': 91},
# 5: {'Anna': 68, 'Bob': 60, 'Joe': 67, 'May': 70},
# 6: {'Anna': 8, 'Bob': 4, 'Joe': 3, 'May': 2},
# 7: {'Anna': 13, 'Bob': 11, 'Joe': 10, 'May': 12}}
game = int(input("Please enter the number of a game: "))
# now print the first three names from the game:
for place, (name, score) in enumerate( sorted(games[game].items(), key=lambda k: k[1], reverse=True)[:3], 1 ): # <-- only first three places
print('{}. {:<10} {}'.format(place, name, score))
打印:
Please enter the number of a game: 4
1. Bob 100
2. Anna 92
3. May 91
你可以这样做:
import numpy as np
players_score =np.array([ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ])
players_rank=list(
map(lambda x: (players_score[x[0], 0], *x[1]+1),
enumerate(players_score[:, 1:].argsort(axis=0))
)
)
输出:
[
('Joe', 1, 4, 4, 2, 2, 3, 1),
('Bob', 3, 1, 1, 1, 1, 1, 2),
('May', 2, 3, 2, 3, 4, 2, 3),
('Anna', 4, 2, 3, 4, 3, 4, 4)
]
我在一个列表中有三个元组,如果我是正确的话,我正在尝试 return 给定游戏的三个最佳玩家的排名。考虑以下因素:
players_score = [ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ]
正在打印第一个游戏的结果 returns 以下比分:
[('Joe', 100), ('Bob', 90), ('May', 80), ('Anna', 95)]
但是,我想让程序打印这个:
[ ('Joe', 1), ('Anna', 2), ('Bob', 3) ]
由于乔有 100 分,安娜有 95 分,鲍勃有 90 分。
我正在考虑使用 enumerate()
,但我认为首先将元组转换为字典可能会更好,我在代码的最后部分尝试过,但运气不佳。
我开始尝试以下方法:
#prints the corresponding score with the player.
game = int(input("Please enter the number of a game: "))
game_score = [ (p[0], p[game]) for p in players_score]
print(game_score)
#prints a list of results ordered from high to low.
score_list = []
for p in players_score:
score_list.append(p[game])
score_list.sort(reverse=True)
print(score_list)
#prints only the name and the result of the first game
list = [ (name, first) for name, first, second, third, fourth, sixth, seventh, eighth in players_score ]
print(list)
#trying to get a convert from a tuple to a dict.
for name, first, second, third, forth, sixth, seventh, eighth in players_score:
dict(name, first)
print(dict)
你有这个:
a = [('Joe', 100), ('Bob', 90), ('May', 80), ('Anna', 95)]
尝试使用每个元组的第二个参数对其进行排序:
a = sorted(a, key=lambda x:x[1], reverse=True)
然后:
result = [(v[0], i+1) for i,v in enumerate(a)][0:3]
输出将是:
[('Joe', 1), ('Anna', 2), ('Bob', 3)]
此外,对于第一个答案,您可以将玩家得分转换为字典,以方便之后的操作:
例如:
players_score = [ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ]
# convert player's scores to dictionary
games = {}
for (player_name, *scores) in players_score:
for game_no, score in enumerate(scores, 1):
games.setdefault(game_no, {}).setdefault(player_name, {})
games[game_no][player_name] = score
# the dictionary "games" will look like:
# {1: {'Anna': 95, 'Bob': 90, 'Joe': 100, 'May': 80},
# 2: {'Anna': 32, 'Bob': 38, 'Joe': 34, 'May': 36},
# 3: {'Anna': 36, 'Bob': 4, 'Joe': 38, 'May': 40},
# 4: {'Anna': 92, 'Bob': 100, 'Joe': 90, 'May': 91},
# 5: {'Anna': 68, 'Bob': 60, 'Joe': 67, 'May': 70},
# 6: {'Anna': 8, 'Bob': 4, 'Joe': 3, 'May': 2},
# 7: {'Anna': 13, 'Bob': 11, 'Joe': 10, 'May': 12}}
game = int(input("Please enter the number of a game: "))
# now print the first three names from the game:
for place, (name, score) in enumerate( sorted(games[game].items(), key=lambda k: k[1], reverse=True)[:3], 1 ): # <-- only first three places
print('{}. {:<10} {}'.format(place, name, score))
打印:
Please enter the number of a game: 4
1. Bob 100
2. Anna 92
3. May 91
你可以这样做:
import numpy as np
players_score =np.array([ ('Joe', 100, 34, 38, 90, 67, 3, 10),
('Bob', 90, 38, 4, 100, 60, 4, 11),
('May', 80, 36, 40, 91, 70, 2, 12),
('Anna', 95, 32, 36, 92, 68, 8, 13) ])
players_rank=list(
map(lambda x: (players_score[x[0], 0], *x[1]+1),
enumerate(players_score[:, 1:].argsort(axis=0))
)
)
输出:
[
('Joe', 1, 4, 4, 2, 2, 3, 1),
('Bob', 3, 1, 1, 1, 1, 1, 2),
('May', 2, 3, 2, 3, 4, 2, 3),
('Anna', 4, 2, 3, 4, 3, 4, 4)
]