0

This might be a rather simple question - but I'm relatively new to python in my defense! Here's the case - I have loaded a CSV file into a list which looks something like this:

["4567", "59.0000", 7.0000", "1"]
["4567", "59.0000", 7.0000", "2"]
["4567", "59.0000", 7.0000", "5"]
["1234", "59.0000", 7.0000", "1"]
["1234", "59.0000", 7.0000", "2"]
["1234", "59.0000", 7.0000", "3"]

The first column is a unique id - where the last is number in the sequence which needs to be preserved (root of my problem).

I would like to order by the unique id (first row), but keep the sequences in order. So I would rather have the list like this

["1234", "59.0000", 7.0000", "1"]
["1234", "59.0000", 7.0000", "2"]
["1234", "59.0000", 7.0000", "3"]
["4567", "59.0000", 7.0000", "1"]
["4567", "59.0000", 7.0000", "2"]
["4567", "59.0000", 7.0000", "5"]

Obviously when I just sort on the unique id - the sequences gets messed up.

Is this a simple task to do all in one, or do I need to loop through the list twice? How can it be done? I'm pretty clueless. Thanks for your time!

2 Answers2

1

This question appears to be "How to sort by multiple values?"

If this is so, see How To: Sorting which covers this and most other cases. The basic approach is to use the key function to return a tuple, as tuples are orderable in Python.

# sort by 1st column, then 4th column (both as integer values)
sorted(rows, key = lambda r: (int(r[0]), int(r[3]))

Now, even if there wasn't the 4th column (or if it is the order of the row and not the value of the 4th column that matters), we could still use the fact that sorted uses is a stable sort. This means that for any items/rows that compare equivalently the ordering is preserved. Thus the following has the same result (for the given data) as the first sort:

# sort by 1st column (as integer); preserve the "row order" by stability
sorted(rows, key = lambda r: int(r[0]))
Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

I recommend using a class instead of a list of lists. But you could also use a key in your sort (sorted is slower than list_.sort()) or (temporarily) change the order of your sublists' elements.

user1277476
  • 2,871
  • 12
  • 10