我有一個 2D 串列,其中分別包含足球運動員的姓名、他們進球的次數以及他們嘗試射門的次數。
player_stats = [['Adam', 5, 10], ['Kyle', 12, 18], ['Jo', 20, 35], ['Adam', 15, 20], ['Charlie', 31, 58], ['Jo', 6, 14], ['Adam', 10, 15]]
從這個串列中,我試圖回傳另一個串列,該串列僅顯示每個玩家的一個實體,以及他們各自的總進球數和總進球數,如下所示:
player_stats_totals = [['Adam', 30, 45], ['Kyle', 12, 18], ['Jo', 26, 49], ['Charlie', 31, 58]]
在 Stack Overflow 上搜索后,我能夠學習(從此執行緒)如何回傳重復播放器的索引
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)
但在此后如何繼續進行,如果這個方法確實與我需要的東西(?)
回傳所需總數串列的最有效方法是什么?
uj5u.com熱心網友回復:
使用字典來累積給定玩家的值:
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()]
是一個串列理解,相當于下面的 for 回圈:
res = []
for player, (first, second) in lookup.items():
res.append([player, first, second])
此外,請閱讀本文以了解拆包。
uj5u.com熱心網友回復:
你想要做的是使用一個字典,其中鍵是球員姓名,值是一個包含 [goals, shot] 的串列。構建它看起來像這樣:
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
然后,如果您想將玩家及其統計資料表示為一個串列,您可以這樣做: list(all_games_stats.items())
uj5u.com熱心網友回復:
您可以將串列轉換為字典。(一旦完成,它總是可以改回來的)這有效:
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)
uj5u.com熱心網友回復:
我也不妨提交一些東西。這是另一種使用串列理解的方法:
# 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:])]
uj5u.com熱心網友回復:
另一種方式,將整個三元組(包括名稱)存盤在字典中并更新它們:
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()]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367465.html
