Python Forum
Sort List of Lists by Column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort List of Lists by Column
#1
Hello all,
I am working on a small Python project and need some help.
I have a CSV file that is structured as follows:

Nr, Name, weight , height, with
112,Aerified, 41.70, 256.99, 141.01
656, Zoolab, 66.64, 108.44, 269.53

There are 10.000 randomly generated rows und the file.

Now I want to sort this file according to a selected column and output it.
For this I don't want to use methods but solve it with an algorithm.
For this I first imported it with pandas and then converted it to a list (hoping to be able to process it better):

frame = pd.concat(li, axis=0, ignore_index=True)
products_list = frame.values.tolist()
That has also worked so far.
I have already tried it with a bubble sort:
def sort(product_list):

  for passnum in range(len(product_list)-1, 0, -1):
     for i in range(passnum):
        if int(product_list[i][2]) > int(product_list[i+1][2]):
            temp = product_list[i]
            product_list[i] = product_list[i+1]
            product_list[i+1] = temp
  return product_list
print(sort(product_list))
The I wanted to sort in this case after the 3rd column . But unfortunately it does not work.
Do you have any other ideas?
Reply
#2
Your first snippet doesn't define li, so I'm not completely sure of what format you have.

Without that or other information, we don't know what "does not work" means. Does it error, does the sort seem wrong? Please give inputs that show what is wrong. When I tried it, it seemed to work. The given data is already sorted on the third column.

That said:
  • You don't need temp variables to swap items. You can instead assign them at the same time like:
    product_list[i], product_list[i+1] = product_list[i+1], product_list[i]
  • Is there a reason you are converting the numbers to int()? As they have decimals, I could see float() if you were converting from text. But otherwise you are losing precision.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 405 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,024 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,563 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,277 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,000 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  How to efficiently average same entries of lists in a list xquad 5 2,070 Dec-17-2021, 04:44 PM
Last Post: xquad
  Not able to add extra column to the list in the python shantanu97 2 1,611 Nov-17-2021, 10:14 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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