如何使用itertools进行范围迭代?

时间:2019-12-16 18:05:02

标签: python dictionary tuples itertools

考虑其中的四个元组中的以下列表:

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) ]

玩家已经玩了7场比赛。在第一局中,乔赢了100分。

我想根据以下各项为每位球员得分:

First/best player: 5 points
Second player: 3 points
Third player: 1 point
Fourth player: -1 point -> But 0 points is what I like to assign the fourth player of each game.

到目前为止,我的代码:

from itertools import zip_longest as y

zipped = list(y(*[game_score for _, *game_score in players_score]))
tup = list(sorted(i, reverse=True) for i in zipped)
score_dict = {}
for player, *game_score in players_score:
    tmp = []
    for i,j in zip(game_score, tup):
        tmp.append(5-2*j.index(i))
    tot_score = sum(tmp)
    score_dict[player] = tot_score

print("The overall scores are: ", sorted(score_dict.items(), key=lambda kv: (kv[1], kv[0]), reverse=True))

因此,我的代码将-1分应用于每个游戏的第四名玩家,但我希望第四名玩家仅获得0个零点。 我正在努力应用范围([0:3])或另一种方法,我可以跳过第四名球员的得分,因为该球员仅获得0分。

4 个答案:

答案 0 :(得分:0)

您可以更改评分功能:

tmp.append(5-2*j.index(i) if 5-2*j.index(i) > 0 else 0)

因此,如果存在负值,请将分数设置为0

答案 1 :(得分:0)

一个非常简单的解决方案

for i,j in zip(game_score, tup):
        score_var = 5-2*j.index(i)
        if (score_var < 0):
              score_var = 0
        tmp.append(score_var)

这将确保没有玩家获得负分。

答案 2 :(得分:0)

使用max(0, x)来确保该值至少为0

tmp.append(max(0, 5-2*j.index(i)))

答案 3 :(得分:0)

我会对此进行一些不同的设置。话虽如此,有多种方法可以做到这一点。我要做的第一件事是将players_score列表变成一个更漂亮的集合,例如字典-这样操作起来会更有趣。

基本上,我们遍历“回合”(游戏),然后根据当前回合中的得分对球员姓名进行排序(本回合的获胜者排在第一位)。然后,我们zip会按照他们应得的奖励(分数)对已排序的玩家名称进行排序,并使用玩家名称作为关键字来更新我们的collections.Counter集合。

from collections import Counter

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)
]

original_scores = {player: scores for player, *scores in players_score}

def iter_score(player):
    for score in original_scores[player]:
        yield player, score

score_rewards = [5, 3, 1, 0]

overall_scores = Counter()

for current_round in zip(*map(iter_score, original_scores)):
    sorted_players = map(lambda tpl: tpl[0], sorted(current_round, key=lambda tpl: tpl[1], reverse=True))
    for player, score in zip(sorted_players, score_rewards):
        overall_scores.update({player: score})

print("The overall scores:")
for player, score in overall_scores.most_common():
    print(f"{player}: {score}")

输出:

The overall scores:
Anna: 20
May: 17
Bob: 15
Joe: 11