Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to custom order a list?

Obs: I know lists in python are not order-fixed, but think that this one will be. And I'm using Python 2.4

I have a list, like (for example) this one:

mylist = [ ( u'Article', {"...some_data..."}    ) ,
           ( u'Report' , {"...some_data..."}    ) ,
           ( u'Book'   , {"...another_data..."} ) ,
...#continue
]

This variable mylist is obtained from a function, and the 'order' of the list returned will vary. So, sometimes it will be like on the example. Sometimes, the 'Report' will come before 'Article', etc.

I have a fixed order that I want on this list (and isn't the alphabetical).

Let's say that my fixed order is: 'Report', 'Article', 'Book', ...

So, what I want is that: whatever order 'mylist' is instantiated, I want to reorder it making 'Report' stay on front, 'Article' on second, etc...

What's the best approach to reorder my list (taking the first element of the tuple of each item on list) using my 'custom' order?

Answer:

I ended up with this:

mylist became a list of dicts, like this:

mylist = [{'id':'Article', "...some_data..."} ,
        ...etc
]

each dict having a 'id' that had to be sorted.

Saving the correct order on a listAssigning the correct_order on a list:

correct_order = ['Report', 'Article', 'Book', ...]

and doing:

results = sorted([item for item in results], cmp=lambda x,y:cmp(correct_order.index(x['id']), correct_order.index(y['id'])))
like image 728
Gabriel L. Oliveira Avatar asked Sep 02 '10 06:09

Gabriel L. Oliveira


People also ask

What is the fastest way to sort a list in Python?

Quicksort. Quicksort is an incredibly efficient algorithm that uses the divide and conquer methodology to sort the list with as little passes as possible. By choosing a pivot element (in this case, the first element), it will place everything lower than that element on the left side and everything higher on the right.

How do you sort a list in Python program?

Python list sort() function can be used to sort a List in ascending, descending, or user-defined order. In each case, the time complexity is O(nlogn) in Python.

Does Python sort modify the list?

The sort() method is a list method that modifies the list in-place and returns None. In other words, the sort() method modifies or changes the list it is called on, and does not create a new list.


2 Answers

You could use a dictionary that would map every first element to its "weight" and then check this dictionary inside a sorting function.

Something like:

d = { "Report": 1,
      "Article": 2,
       "Book": 3 }
result = sorted(mylist, key=lambda x:d[x[0]])
like image 129
Joril Avatar answered Oct 01 '22 15:10

Joril


You could use a dictionary, that would allow you to access "Book", "Article", etc. without having to care about the order. I would put the data from that list into a dict that look like this:

mydict = { u'Article': "somedata",
           u'Report': "someotherdata", ...}

If you really want to sort your list in the way you described, you can use the list.sort with a key function that represents your particular sort order (Documentation). You need the key function as you need to access only the first element and your sorting order also is not alphabetical.

like image 45
Mad Scientist Avatar answered Oct 01 '22 13:10

Mad Scientist