如何使用itertools进行范围迭代? [英] How to iterate with range using itertools?

查看:142
本文介绍了如何使用itertools进行范围迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Considering the following list within it 4 tuples:

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分.

The players have been playing 7 games. In the first game, Joe has won with 100 points.

我想根据以下各项为(每场比赛)的每位球员得分:

I would like to score each player (of each game) according to the following:

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.

到目前为止,我的代码:

My code so far:

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分.

So, my code applies -1 points to the fourth player of each game, but instead, I want the fourth player to earn just 0 zero points. I'm struggling with applying a range ([0:3]) or another method by which I could skip the fourth player from getting a score because that player just earns 0 points.

推荐答案

我会对此进行一些设置.话虽这么说,有多种方法可以做到这一点.我要做的第一件事是将players_score列表变成一个更漂亮的集合,就像字典一样-只是工作起来更有趣.

I would set this up a bit differently. That being said, there's more than one way to do this. The first thing I would do is turn that players_score list into a prettier collection, like a dictionary - just so it's more fun to work with.

基本上,我们遍历我们的回合"(游戏),然后根据当前回合中的得分对玩家名称进行排序(本回合的赢家排在第一位).然后,我们zip排序的玩家名称及其应得的奖励(分数),并使用玩家名称作为键来更新我们的collections.Counter集合.

Basically, we iterate through our "rounds" (the games), and sort the player names based on their score in the current round (the winner of the current round comes first). Then we zip the sorted player names with the respective reward (score) they deserve, and update our collections.Counter collection, using the player name as the key.

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

这篇关于如何使用itertools进行范围迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆