Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom sort python

Tags:

python

sorting

I have a question: This is list of lists, formed by ElementTree library.

   [['word1', <Element tag at b719a4cc>], ['word2', <Element tag at b719a6cc>], ['word3', <Element tag at b719a78c>], ['word4', <Element tag at b719a82c>]]

word1..4 may contain unicode characters i.e (â,ü,ç).

I want to sort this list of lists by my custom alphabet.

I know how to sort by custom alphabet from here sorting words in python

I also know how to sort by key from here http://wiki.python.org/moin/HowTo/Sorting

The problem is that I couldn't find the way how to apply these two method to sort my "list of lists".

like image 491
microspace Avatar asked May 18 '12 02:05

microspace


People also ask

How do I create a custom sort in Python?

In Python, we can write custom sort functions that work with sort() and sorted() . The value of the key parameter should be a function that takes a single argument and returns a key for sorting purposes.

How do you customize a sort function?

To define custom sort function, you need to compare first value with second value. If first value is greater than the second value, return -1. If first value is less than the second value, return 1 otherwise return 0. The above process will sort the data in descending order.

How do you sort a list in custom order in Python?

Using sort(), lamba, index(): The sort() function does the required in-place sorting(without creating a separate list to store the sorted order) along with a lambda function with a key to specify the function execution for each pair of tuples, the index() function helps to get the order from our custom list list_2.

How do you sort a DataFrame in a specific order in Python?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.


2 Answers

Your first link more or less solves the problem. You just need to have the lambda function only look at the first item in your list:

alphabet = "zyxwvutsrqpomnlkjihgfedcba"

new_list = sorted(inputList, key=lambda word: [alphabet.index(c) for c in word[0]])

One modification I might suggest, if you're sorting a reasonably large list, is to change the alphabet structure into a dict first, so that index lookup is faster:

alphabet_dict = dict([(x, alphabet.index(x)) for x in alphabet)
new_list = sorted(inputList, key=lambda word: [alphabet_dict[c] for c in word[0]])
like image 68
happydave Avatar answered Oct 18 '22 22:10

happydave


If I'm understanding you correctly, you want to know how to apply the key sorting technique when the key should apply to an element of your object. In other words, you want to apply the key function to 'wordx', not the ['wordx', ...] element you are actually sorting. In that case, you can do this:

my_alphabet = "..."

def my_key(elem):
    word = elem[0]
    return [my_alphabet.index(c) for c in word]

my_list.sort(key=my_key)

or using the style in your first link:

my_alphabet = "..."
my_list.sort(key=lambda elem: [my_alphabet.index(c) for c in elem[0]])

Keep in mind that my_list.sort will sort in place, actually modifying your list. sorted(my_list, ...) will return a new sorted list.

like image 23
KP. Avatar answered Oct 18 '22 22:10

KP.