Python - 二维列表 - 在一列中查找重复项并在另一列中求和值

Python - 2D list - find duplicates in one column and sum values in another column

我有一个二维列表,其中分别包含足球运动员的姓名、他们进球的次数以及他们尝试射门的次数。

player_stats = [['Adam', 5, 10], ['Kyle', 12, 18], ['Jo', 20, 35], ['Adam', 15, 20], ['Charlie', 31, 58], ['Jo', 6, 14], ['Adam', 10, 15]]

从这个列表中,我正在尝试 return 另一个列表,该列表仅显示每个玩家的一个实例及其各自的 个目标和 总计 次射门尝试,如下所示:

player_stats_totals = [['Adam', 30, 45], ['Kyle', 12, 18], ['Jo', 26, 49], ['Charlie', 31, 58]]

在 Stack Overflow 上搜索后,我能够(从 )学习如何 return 重复玩家的索引

x = [player_stats[i][0] for i in range (len(player_stats))]

for i in range (len(x)):
    if (x[i] in x[:i]) or (x[i] in x[i+1:]): print (x[i], i)

但在之后的处理方式上卡住了,如果这个方法确实与我需要的完全相关(?)

return 所需总计列表的最有效方法是什么?

使用字典来累积给定玩家的值:

player_stats = [['Adam', 5, 10], ['Kyle', 12, 18], ['Jo', 20, 35], ['Adam', 15, 20], ['Charlie', 31, 58], ['Jo', 6, 14], ['Adam', 10, 15]]

lookup = {}
for player, first, second in player_stats:
    
    # if the player has not been seen add a new list with 0, 0 
    if player not in lookup:
        lookup[player] = [0, 0]
    
    # get the accumulated total so far 
    first_total, second_total = lookup[player]
    
    # add the current values to the accumulated total, and update the values 
    lookup[player] = [first_total + first, second_total + second]

# create the output in the expected format
res = [[player, first, second] for player, (first, second) in lookup.items()]
print(res)

输出

[['Adam', 30, 45], ['Kyle', 12, 18], ['Jo', 26, 49], ['Charlie', 31, 58]]

一个更高级的,pythonic,版本是用collections.defaultdict:

from collections import defaultdict

player_stats = [['Adam', 5, 10], ['Kyle', 12, 18], ['Jo', 20, 35],
                ['Adam', 15, 20], ['Charlie', 31, 58], ['Jo', 6, 14], ['Adam', 10, 15]]

lookup = defaultdict(lambda: [0, 0])
for player, first, second in player_stats:
    # get the accumulated total so far
    first_total, second_total = lookup[player]

    # add the current values to the accumulated total, and update the values
    lookup[player] = [first_total + first, second_total + second]

# create the output in the expected format
res = [[player, first, second] for player, (first, second) in lookup.items()]

print(res)

这种方法的优点是跳过 初始化。两种方法都是 O(n)。

备注

表达式:

res = [[player, first, second] for player, (first, second) in lookup.items()]

是一个list comprehension,相当于下面的for循环:

res = []
for player, (first, second) in lookup.items():
    res.append([player, first, second])

此外,阅读 this 以了解解包。

你想要做的是使用一个字典,其中键是球员姓名,值是包含 [goals, shots] 的列表。构建它看起来像这样:

all_games_stats = {}
for stat in player_stats:
    player, goals, shots = stat
    if player not in all_games_stats:
        all_games_stats[player] = [goals, shots]
    else:
        stat_list = all_games_stats[player]
        stat_list[0] += goals
        stat_list[1] += shots

然后,如果您想将球员及其统计数据表示为列表,您可以这样做: 列表(all_games_stats.items())

您可以将列表转换为字典。 (它总是可以在完成后改回)这个有效:

player_stats = [['Adam', 5, 10], ['Kyle', 12, 18], ['Jo', 
20, 35], ['Adam', 15, 20], ['Charlie', 31, 58], ['Jo', 6, 
14], ['Adam', 10, 15]]

new_stats = {}


for item in player_stats:
    if not item[0] in new_stats:
        new_stats[item[0]] = [item[1],item[2]]
    else:
        new_stats[item[0]][0] += item[1]
        new_stats[item[0]][1] += item[2]

print(new_stats)

我也不妨提交一些东西。这是另一种具有一些列表理解功能的方法:

# Unique values to new dictionary with goal and shots on goal default entries 
agg_stats = dict.fromkeys(set([p[0] for p in player_stats]), [0, 0])

# Iterate over the player stats list
for player in player_stats:
    # Set entry to sum of current and next stats values for the corresponding player.
    agg_stats[player[0]] = [sum([agg_stats.get(player[0])[i], stat]) for i, stat in enumerate(player[1:])]

另一种方式,将整个三元组(包括名称)存储在字典中并更新它们:

stats = {}
for name, goals, attempts in player_stats:
    entry = stats.setdefault(name, [name, 0, 0])
    entry[1] += goals
    entry[2] += attempts
player_stats_totals = list(stats.values())

还有一个有趣的复数解决方案,这使得添加很好但需要烦人的转换回来:

from collections import defaultdict

tmp = defaultdict(complex)
for name, *stats in player_stats:
    tmp[name] += complex(*stats)
player_stats_totals = [[name, int(stats.real), int(stats.imag)]
                       for name, stats in tmp.items()]