按第一个值排序元组列表

时间:2016-11-07 19:59:36

标签: python list sorting tuples

我希望按每个元组中的第一个值对列表进行排序,但是我的代码不生成任何实际排序列表,尽管列表的顺序确实发生了变化。我的列表包含元组,其中包含' PlayerCode'这是每个玩家的唯一键。和平均积分'这是玩家在每场比赛中得分的平均分数。我希望按照“平均积分”排序此列表。虽然我似乎无法做对。这是我的代码

def getKey(item):
        return item[0]
def sortByPoints():
    foundPlayers = []
    with open ('PlayerList.csv') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            foundPlayers.append((row['Average PTS'], row['PlayerCode']))
    print(foundPlayers)
    sortedPlayers = sorted(foundPlayers, key = getKey)

    print (sortedPlayers)

就像我说的,这确实改变了列表的顺序,但不是我想要的方式,也不能看到它改变它的任何真实模式。 我正在使用python 3.4

这是两个列表的输出

未排序: [(' 7',' 1'),(' 8',' 2'),(' 4' ,' 3'),(' 28',' 4'),(' 0',' 5') ,(' 18',' 6'),(' 10',' 7'),(' 9' ,' 8'),(' 10',' 9'),(' 2',' 10') ,(' 4',' 11'),(' 2',' 12'),(' 3' ,' 13'),(' 4',' 14'),(' 2',' 15') ]

排序方法: [(' 0',' 5'),(' 10',' 7'),(' 10' ,' 9'),(' 18',' 6'),(' 2',' 10') ,(' 2',' 12'),(' 2',' 15'),(' 28' ,' 4'),(' 3',' 13'),(' 4',' 3') ,(' 4',' 11'),(' 4',' 14'),(' 7' ,' 1'),(' 8',' 2'),(' 9',' 8') ]

5 个答案:

答案 0 :(得分:0)

问题是你有字符串,所以它按字母顺序排序。转换为整数:

perl -nE '/\bn= (\d+).*\bperf= ([\d.]+)/ and say "$1 $2"' * > output

答案 1 :(得分:0)

row['Average PTS']row['Player Code']返回字符串。 Python会将字符串与(可能)令人惊讶的结果进行比较

>>> '6' > '42'
True

使用row['Average PTS']转换row['Player Code']int

答案 2 :(得分:0)

您可以使用从内置模块 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { if let selectedRow = activityTableView.indexPathForSelectedRow { updateCell(atIndexPath: selectedRow) } var rowActions = [UITableViewRowAction]() .... return rowActions }

导入的itemgetter

示例代码:

operator

输出:

from operator import itemgetter

tuples = [(10, 3), (1, 4), (5, 4)]
sorted_tuples = sorted(tuples, key=itemgetter(0))

print(sorted_tuples)

答案 3 :(得分:0)

您可以使用lambda函数作为键进行排序:

sorted(my_list, key=lambda x: (int(x[1]), int(x[0])))

下面:

  • list将根据1 st索引的内容进行排序。如果值相同,list将根据0索引进行排序。
  • 您还需要将tuple中的值输入到int。排序按字典顺序排序。这意味着,对于字符串值,' 11'将会出现在' 2'。
  • 之前

以下是示例示例:

>>> my_list = [('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]
>>> sorted(my_list, key=lambda x: (int(x[1]), int(x[0])))                                  
[('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]

答案 4 :(得分:0)

这是我的答案。我相信尽可能保持OP的原始代码,以尽可能减少假设。如果需要有字符串怎么办?此外,尽可能减少额外的进口。因此我的解决方案是:

def sortByPoints():
    foundPlayers = []
    with open ('PlayerList.csv') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            foundPlayers.append((row['Average PTS'], row['PlayerCode']))
    print(foundPlayers)
    sortedPlayers = sorted(foundPlayers, key=lambda x: float(x[0]))
    print(sortedPlayers)

删除多余的getKey功能,并使用lambda代替整洁。使用float以防将来可能需要(因为您仅用于比较;使用int仅限制您,并且优于float)。

我也相信复制和粘贴代码;)

仅供参考,改变方向:

sortedPlayers = sorted(foundPlayers, key=lambda x: float(x[0]), reverse=True)

另请注意,如果意图确实仅适用于整数,那么您应该按照每个人的建议进行操作并将您的元素转换为int

def sortByPoints():
    foundPlayers = []
    with open ('PlayerList.csv') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            foundPlayers.append((int(row['Average PTS']), int(row['PlayerCode'])))
    print(foundPlayers)
    sortedPlayers = sorted(foundPlayers, key=lambda x: x[0])
    print(sortedPlayers)
相关问题