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. 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.
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.
No comments:
Post a Comment