简体   繁体   English

排序二维列表python

[英]sort 2-D list python

I'm relatively new to programming, and I want to sort a 2-D array (lists as they're called in Python) by the value of all the items in each sub-array. 我对编程比较陌生,我想用每个子数组中所有项的值对二维数组(在Python中调用它们的列表)进行排序。 For example: 例如:

pop = [[1,5,3],[1,1,1],[7,5,8],[2,5,4]]

The sum of the first element of pop would be 9, because 1 + 5 + 3 = 9. The sum of the second would be 3, because 1 + 1 + 1 = 3, and so on. pop的第一个元素的总和为9,因为1 + 5 + 3 =9。第二个元素的总和为3,因为1 +1 + 1 = 3,依此类推。

I want to rearrange this so the new order would be: 我想重新排列,以便新的顺序是:

newPop = [pop[1], pop[0], pop[3], pop[2]]

How would I do this? 我该怎么做?

Note: I don't want to sort the elements each sub-array, but sort according to the sum of all the numbers in each sub-array. 注意:我不想对每个子数组中的元素进行排序,而是根据每个子数组中所有数字的总和进行排序。

You can use sorted() : 你可以使用sorted()

>>> pop = [[1,5,3],[1,1,1],[7,5,8],[2,5,4]]
>>> newPop = sorted(pop, key=sum)
>>> newPop
[[1, 1, 1], [1, 5, 3], [2, 5, 4], [7, 5, 8]]

You can also sort in-place with pop.sort(key=sum) . 您也可以使用pop.sort(key=sum)进行就地排序。 Unless you definitely want to preserve the original list, you should prefer in-pace sorting. 除非您确实要保留原始列表,否则应选择按步排序。

Try this: 尝试这个:

sorted(pop, key=sum)

Explanation: 说明:

  • The sorted() procedure sorts an iterable (a list in this case) in ascending order sorted()过程按升序对可迭代对象(在这种情况下为列表)进行排序
  • Optionally, a key parameter can be passed to determine what property of the elements in the list is going to be used for sorting (可选)可以传递key参数以确定列表中元素的哪些属性将用于排序
  • In this case, the property is the sum of each of the elements (which are sublists) 在这种情况下,属性是每个元素(它们是子列表)的sum

So essentially this is what's happening: 所以基本上这就是发生的事情:

[[1,5,3], [1,1,1], [7,5,8], [2,5,4]]              # original list
[sum([1,5,3]), sum([1,1,1]), sum([7,5,8]), sum([2,5,4])] # key=sum
[9, 3, 20, 11]                                    # apply key
sorted([9, 3, 20, 11])                            # sort
[3, 9, 11, 20]                                    # sorted
[[1,1,1], [1,5,3], [2,5,4], [7,5,8]]              # elements coresponding to keys

@arshajii beat me to the punch, and his answer is good. @arshajii打败了我,他的答案很好。 However, if you would prefer an in-place sort: 但是,如果您希望就地排序:

>>> pop = [[1,5,3],[1,1,1],[7,5,8],[2,5,4]]
>>> pop.sort(key=sum)
>>> pop
[[1, 1, 1], [1, 5, 3], [2, 5, 4], [7, 5, 8]]

I have to look up Python's sorting algorithm -- I think it's called Timsort, bit I'm pretty sure an in-place sort would be less memory intensive and about the same speed. 我必须查找Python的排序算法 - 我认为它被称为Timsort,我非常确定就地排序会减少内存密集度并且速度相同。

Edit: As per this answer, I would definitely recommend x.sort() 编辑:根据这个答案,我肯定会推荐x.sort()

If you wanted to sort the lists in a less traditional way, you could write your own function (that takes one parameter.) At risk of starting a flame war, I would heavily advise against lambda . 如果你想以一种不那么传统的方式对列表进行排序,你可以编写自己的函数(需要一个参数。)冒着开始火焰战争的风险,我会大力反对lambda

For example, if you wanted the first number to be weighted more heavily than the second number more heavily than the third number, etc: 例如,如果您希望第一个数字的权重大于第二个数字,则比第三个数字更重要,等等:

>>> def weightedSum(listToSum):
...     ws = 0
...     weight = len(listToSum)
...     for i in listToSum:
...         ws += i * weight
...         weight -= 1
...     return ws
... 
>>> weightedSum([1, 2, 3])
10
>>> 1 * 3 + 2 * 2 + 3 * 1
10
>>> pop
[[1, 5, 3], [1, 1, 1], [7, 5, 8], [2, 5, 4]]
>>> pop.sort(key=weightedSum)
>>> pop
[[1, 1, 1], [1, 5, 3], [2, 5, 4], [7, 5, 8]]
>>> pop += [[1, 3, 8]]
>>> pop.sort(key=weightedSum)
>>> pop
[[1, 1, 1], [1, 5, 3], [1, 3, 8], [2, 5, 4], [7, 5, 8]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM