我正在尝试在程序中设置某个数据结构,该结构从excel文件获取数据。需要按照以下标准进行排序:
目前,我已经能够通过按索引5的顺序排序来解决这个问题:
[8, 8, 8, 8, 0, 250]
[8, 8, 8, 0, 0, 50]
[8, 8, 0, 0, 0, 5]
[1, 1, 1, 1, 1, 50]
[1, 1, 1, 1, 0, 25]
[1, 1, 1, 0, 0, 4]
[2, 2, 2, 2, 2, 60]
[2, 2, 2, 2, 0, 30]
[2, 2, 2, 0, 0, 5]
[3, 3, 3, 3, 3, 70]
[3, 3, 3, 3, 0, 35]
[3, 3, 3, 0, 0, 8]
[4, 4, 4, 4, 4, 80]
[4, 4, 4, 4, 0, 40]
[4, 4, 4, 0, 0, 10]
[5, 5, 5, 5, 5, 90]
[5, 5, 5, 5, 0, 45]
[5, 5, 5, 0, 0, 12]
[6, 6, 6, 6, 6, 100]
[6, 6, 6, 6, 0, 50]
[6, 6, 6, 0, 0, 15]
[9, 9, 9, 9, 9, 120]
[9, 9, 9, 9, 0, 60]
[9, 9, 9, 0, 0, 20]
[9, 9, 0, 0, 0, 2]
[10, 10, 10, 10, 10, 150]
[10, 10, 10, 10, 0, 75]
[10, 10, 10, 0, 0, 25]
[10, 10, 0, 0, 0, 3]
[11, 11, 11, 11, 11, 400]
[11, 11, 11, 11, 0, 150]
[11, 11, 11, 0, 0, 40]
[11, 11, 0, 0, 0, 3]
进入此
[11, 11, 11, 11, 11, 400]
[8, 8, 8, 8, 0, 250]
[11, 11, 11, 11, 0, 150]
[10, 10, 10, 10, 10, 150]
[9, 9, 9, 9, 9, 120]
[6, 6, 6, 6, 6, 100]
[5, 5, 5, 5, 5, 90]
[4, 4, 4, 4, 4, 80]
[10, 10, 10, 10, 0, 75]
[3, 3, 3, 3, 3, 70]
[9, 9, 9, 9, 0, 60]
[2, 2, 2, 2, 2, 60]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
[5, 5, 5, 5, 0, 45]
[11, 11, 11, 0, 0, 40]
[4, 4, 4, 4, 0, 40]
[3, 3, 3, 3, 0, 35]
[2, 2, 2, 2, 0, 30]
[10, 10, 10, 0, 0, 25]
[1, 1, 1, 1, 0, 25]
[9, 9, 9, 0, 0, 20]
[6, 6, 6, 0, 0, 15]
[5, 5, 5, 0, 0, 12]
[4, 4, 4, 0, 0, 10]
[3, 3, 3, 0, 0, 8]
[2, 2, 2, 0, 0, 5]
[8, 8, 0, 0, 0, 5]
[1, 1, 1, 0, 0, 4]
[11, 11, 0, 0, 0, 3]
[10, 10, 0, 0, 0, 3]
[9, 9, 0, 0, 0, 2]
这使用了.sort(key = lambda x:x[5])
和.reverse()
函数。
但是,请特别注意一个部分。
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
我希望索引0为8的任何东西都列在第一位,所以应该是
[8, 8, 8, 0, 0, 50]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
我知道在这种特定情况下,可以通过对索引0进行二次排序来获得所需的结果来完成此操作。但是,例如在某些情况下,我们可能会有不同的输入
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
[11, 11, 0, 0, 0, 50]
需要分类的
[8, 8, 8, 0, 0, 50]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[11, 11, 0, 0, 0, 50]
因此,对辅助变量进行排序是行不通的。我可以从哪里开始寻找针对此特定问题的解决方案?
答案 0 :(得分:1)
尝试一下:
l = [[8, 8, 8, 8, 0, 250], [8, 8, 8, 0, 0, 50], [8, 8, 0, 0, 0, 5], [1, 1, 1, 1, 1, 50], [1, 1, 1, 1, 0, 25], [1, 1, 1, 0, 0, 4], [2, 2, 2, 2, 2, 60], [2, 2, 2, 2, 0, 30], [2, 2, 2, 0, 0, 5], [3, 3, 3, 3, 3, 70], [3, 3, 3, 3, 0, 35], [3, 3, 3, 0, 0, 8], [4, 4, 4, 4, 4, 80], [4, 4, 4, 4, 0, 40], [4, 4, 4, 0, 0, 10], [5, 5, 5, 5, 5, 90], [5, 5, 5, 5, 0, 45], [5, 5, 5, 0, 0, 12], [6, 6, 6, 6, 6, 100], [6, 6, 6, 6, 0, 50], [6, 6, 6, 0, 0, 15], [9, 9, 9, 9, 9, 120], [9, 9, 9, 9, 0, 60], [9, 9, 9, 0, 0, 20], [9, 9, 0, 0, 0, 2], [10, 10, 10, 10, 10, 150], [10, 10, 10, 10, 0, 75], [10, 10, 10, 0, 0, 25], [10, 10, 0, 0, 0, 3], [11, 11, 11, 11, 11, 400], [11, 11, 11, 11, 0, 150], [11, 11, 11, 0, 0, 40], [11, 11, 0, 0, 0, 3], [11, 11, 0, 0, 0, 50]]
ls = sorted(l, key = lambda x: [x[5], x[0]==8], reverse = True)
给出的输出为:
[[11, 11, 11, 11, 11, 400], [8, 8, 8, 8, 0, 250], [10, 10, 10, 10, 10, 150], [11, 11, 11, 11, 0, 150], [9, 9, 9, 9, 9, 120], [6, 6, 6, 6, 6, 100], [5, 5, 5, 5, 5, 90], [4, 4, 4, 4, 4, 80], [10, 10, 10, 10, 0, 75], [3, 3, 3, 3, 3, 70], [2, 2, 2, 2, 2, 60], [9, 9, 9, 9, 0, 60], [8, 8, 8, 0, 0, 50], [1, 1, 1, 1, 1, 50], [6, 6, 6, 6, 0, 50], [11, 11, 0, 0, 0, 50], [5, 5, 5, 5, 0, 45], [4, 4, 4, 4, 0, 40], [11, 11, 11, 0, 0, 40], [3, 3, 3, 3, 0, 35], [2, 2, 2, 2, 0, 30], [1, 1, 1, 1, 0, 25], [10, 10, 10, 0, 0, 25], [9, 9, 9, 0, 0, 20], [6, 6, 6, 0, 0, 15], [5, 5, 5, 0, 0, 12], [4, 4, 4, 0, 0, 10], [3, 3, 3, 0, 0, 8], [8, 8, 0, 0, 0, 5], [2, 2, 2, 0, 0, 5], [1, 1, 1, 0, 0, 4], [10, 10, 0, 0, 0, 3], [11, 11, 0, 0, 0, 3], [9, 9, 0, 0, 0, 2]]
它将基于列表的第五项作为第一优先级进行排序,如果它们相等,请检查第二优先级-列表的第一项是否为8(首先将具有8的列表放在第一个索引处,然后将按第一项的排序顺序),如:
[8, 8, 8, 0, 0, 50],
[1, 1, 1, 1, 1, 50],
[6, 6, 6, 6, 0, 50],
[11, 11, 0, 0, 0, 50],
答案 1 :(得分:0)
如果使用data.sort(key = lambda x:x [0] == 8),则它将对位置0到8的每个数字进行排序。因此,您可以执行以下操作:
首先对它们进行排序,所以位置0处的所有8个都在末尾:
data.sort(key = lambda x:x[0] == 8)
然后根据位置5进行排序:
data.sort(key = lambda x:x[5])
然后颠倒顺序:
data.reverse()