Python Forum
sorting 2D lists by column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting 2D lists by column
#3
Well, that is embarrasing.
Thank you! If I change 2 to 1 it works in the simple example.

Originally I wanted to sort a list out of a file and there it still doesn't work.
I'll show you the whole code. Maybe you can help?
I'd honestly really appreciate that!

So this is the code:

with open('example.txt','r') as textFile2:
    data_list = [line.split() for line in textFile2]
textFile2.close()

data_list = sorted(data_list, key=lambda data_list: data_list[1])

print(data_list)
and this is how the example.txt file looks like:
16	43	23.5	85.8
17	61	21.1	86.4
18	40	22.5	90.0
19	37	28.5	86.1
20	22	19.4	78.9
21	156	29.2	89.2
22	63	35.4	86.3
23	105	31.3	86.7
This is the output:
Output:
[['23', '105', '31.3', '86.7'], ['21', '156', '29.2', '89.2'], ['20', '22', '19.4', '78.9'], ['19', '37', '28.5', '86.1'], ['18', '40', '22.5', '90.0'], ['16', '43', '23.5', '85.8'], ['17', '61', '21.1', '86.4'], ['22', '63', '35.4', '86.3']]
It works for the numbers with 2 letters, but it still puts 105 and 156 in the front and I don't know why.
Do you understand that?
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