查找每场比赛的分数

2024-09-24 02:21:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试添加每个玩家分配给他的分数项的数量,并且发现返回每个玩家分配给他的分数是有问题的。我的代码如下,但我似乎无法跨越最后的障碍:

with open("players.dat") as f:
    group = []
    for line in f:
        fields = line.split()
        group.append( (fields[0], int(fields[1])))
        print(group)

from collections import deque

player_stats = {}
with open("players.dat") as f:
    for line in f:
        name, score = line.split()
        player_stats.setdefault(name, deque(maxlen=3))  
        player_stats[name].append(int(score))

print(player_stats)
print(len(score))

player_totals = {name: sum(scores) for name, scores in player_stats.items()} 
print(player_totals)

player_totals = {name: max(scores) for name, scores in player_stats.items()} 
print(player_totals)

player_totals = {name: min(scores) for name, scores in player_stats.items()} 
print(player_totals)

#testing counting number of scores for each player
#from collections import Counter
#items = Counter(val[1] for val in player_stats.values())
#print(items)

player_totals = {name:len(score)}
print(player_totals)

我的数据文件是:

rooney 12
rooney 23
rooney 56
rooney 27
ronaldo 14
ronaldo 34
messi 23
messi 45
messi 12
messi 56

我基本上是想找出每个球员的平均得分。你知道吗


Tags: nameinfieldsforstatslinegroupitems
1条回答
网友
1楼 · 发布于 2024-09-24 02:21:09

^{} documentation

In addition to the above, deques support iteration, pickling, len(d), reversed(d), copy.copy(d), copy.deepcopy(d), membership testing with the in operator, and subscript references such as d[-1].

强调我的。你知道吗

要获得长度,只需使用len(scores)

player_averages = {name: sum(scores) / len(scores) for name, scores in player_stats.items()}

相关问题 更多 >