Python Forum
sorting 2D lists by column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting 2D lists by column
#1
Hello everyone,

I would like to sort a multidimensional list by the second column.

Example:
x = [
    [1,18,2],
    [2,9,4],
    [3,1,1]
    ]

x = sorted(x, key=lambda x: x[2])
print(x)
The output I am looking for should be:
[
[3,1,1],
[2,9,4],
[1,18,2]
]
Unfortunately lamdba sorts like this:
[
[3,1,1],
[1,18,2],
[2,9,4]
]
So lamda does put 18 below 9, because of the '1' in the front.
I did try numpy arrays too, but I just can't find the right way.
Can anyone help me with this problem?

thank you very much!
/ToffieFaye
Reply


Messages In This Thread
sorting 2D lists by column - by ToffieFaye - Sep-24-2019, 01:20 PM
RE: sorting 2D lists by column - by perfringo - Sep-24-2019, 02:15 PM
RE: sorting 2D lists by column - by ToffieFaye - Sep-25-2019, 06:39 AM
RE: sorting 2D lists by column - by buran - Sep-25-2019, 06:42 AM
RE: sorting 2D lists by column - by perfringo - Sep-25-2019, 06:55 AM
RE: sorting 2D lists by column - by ToffieFaye - Sep-25-2019, 06:55 AM
RE: sorting 2D lists by column - by perfringo - Sep-25-2019, 07:08 AM
RE: sorting 2D lists by column - by buran - Sep-25-2019, 07:23 AM
RE: sorting 2D lists by column - by ToffieFaye - Sep-25-2019, 07:30 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020