Python:获取二维列表中的前 4 个最高值

Python: Getting the top 4 Highest Values in a 2d List

好的,这可能类似于此线程:Finding 4 highest values from an array

但我不知道如何使用 2d 列表,所以这是我的问题: 我有一个列表:

teams = [Randomteam1,Randomteam2,Randomteam3,Randomteam4,Randomteam5,Randomteam6]

和一个二维列表:

team_info = [[7, 2, 1, 1, 3], [4, 1, 1, 1, 3],[2, 0, 2, 2, 3], [12, 3, 0, 0, 3]],
             [9, 0, 2, 2, 3], [10, 3, 0, 0, 6]]

例如那个列表中的每一个,[7, 2, 1, 1, 3]是一个球队的得分信息,7是多少分,2是多少胜,1负,1平,3是对于玩过的游戏。 team_info 中的每个块都连接到团队中的 1 个团队。例如:

Randomteam1 = [7, 2, 1, 1, 3], Randomteam2 = [4, 1, 1, 1, 3]

等等..

现在我想要 get/display 积分最多的前 4 支球队,以及积分最低的后 2 支球队。

您可以对列表进行排序并使用简单的切片获取元素:

>>> sorted(team_info,key=itemgetter(0),reverse=True)[:4]
[[12, 3, 0, 0, 3], [10, 3, 0, 0, 6], [9, 0, 2, 2, 3], [7, 2, 1, 1, 3]]

>>> sorted(team_info,key=itemgetter(0),reverse=True)[-2:]
[[4, 1, 1, 1, 3], [2, 0, 2, 2, 3]]

如果你也想要球队的名字,你可以使用 zip :

>>> sorted(zip(teams,team_info),key=itemgetter(1,0),reverse=True)[:4]
[('Randomteam4', [12, 3, 0, 0, 3]), ('Randomteam6', [10, 3, 0, 0, 6]), ('Randomteam5', [9, 0, 2, 2, 3]), ('Randomteam1', [7, 2, 1, 1, 3])]
>>> 
>>> zip(*sorted(zip(teams,team_info),key=itemgetter(1,0),reverse=True)[:4])[0]
('Randomteam4', 'Randomteam6', 'Randomteam5', 'Randomteam1')

也可以用heapq.nlargestnsmallest得到N个最大和最小的元素:

>>> import heapq
>>> heapq.nlargest(4,team_info,key=itemgetter(0))
[[12, 3, 0, 0, 3], [10, 3, 0, 0, 6], [9, 0, 2, 2, 3], [7, 2, 1, 1, 3]]

>>> heapq.nsmallest(2,team_info,key=itemgetter(0))
[[2, 0, 2, 2, 3], [4, 1, 1, 1, 3]]

我可能会使用字典,但您可以将列表压缩成对然后排序:

teams = ["Randomteam1","Randomteam2","Randomteam3","Randomteam4","Randomteam5","Randomteam6"]


team_info = [[7, 2, 1, 1, 3], [4, 1, 1, 1, 3],[2, 0, 2, 2, 3], [12, 3, 0, 0, 3], [9, 0, 2, 2, 3], [10, 3, 0, 0, 6]]


data = sorted(zip(teams,team_info),key= lambda x: x[1][0],reverse=True)

[('Randomteam4', [12, 3, 0, 0, 3]), ('Randomteam6', [10, 3, 0, 0, 6]), ('Randomteam5', [9, 0, 2, 2, 3]), ('Randomteam1', [7, 2, 1, 1, 3]), ('Randomteam2', [4, 1, 1, 1, 3]), ('Randomteam3', [2, 0, 2, 2, 3])]

结果是球队按照分数从高到低排序,排序关键字是x[1][0]每对中的第一个元素第二个元素是每个球队的总分。

前 4 名将是 data[:4],最后两支球队将是 data[-2:]

如果您使用字典,则可以轻松关联团队并访问任何 他们的数据,您可以将团队和数据存储为字典并使用排名作为键:

data = {}
srt = sorted(zip(team_info, teams), reverse=True,1)
for ind, (b,a) in enumerate(srt):
    p, w, l, d, gp = b
    data["rank_{}".format(ind)] = {"team":a,"points": p, "wins": w, "loss": l, "draw": d, "games": gp}



from pprint import pprint as pp

pp(data)

输出:

{'rank_1': {'team': 'Randomteam4',
            'draw': 0,
            'games': 3,
            'loss': 0,
            'points': 12,
            'wins': 3},
 'rank_2': {'team': 'Randomteam6',
            'draw': 0,
            'games': 6,
            'loss': 0,
            'points': 10,
            'wins': 3},
 'rank_3': {'team': 'Randomteam5',
            'draw': 2,
            'games': 3,
            'loss': 2,
            'points': 9,
            'wins': 0},
 'rank_4': {'team': 'Randomteam1',
            'draw': 1,
            'games': 3,
            'loss': 1,
            'points': 7,
            'wins': 2},
 'rank_5': {'team': 'Randomteam2',
            'draw': 1,
            'games': 3,
            'loss': 1,
            'points': 4,
            'wins': 1},
 'rank_6': {'team': 'Randomteam3',
            'draw': 2,
            'games': 3,
            'loss': 2,
            'points': 2,
            'wins': 0}}