Python custom type

My question is: This is a list of lists formed by the 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 ie (â, ü, ç).

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 could not find a way to use these two methods to sort my list of lists.

+6
source share
3 answers

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

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

One modification that I could suggest if you are sorting a large enough list is to first change the structure of the alphabet in the dict, so index search 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]]) 
+13
source

If I understand you correctly, you want to know how to apply the key sorting method when the key should be applied to an element of your object. In other words, you want to apply the key function to "wordx" and not to the ['wordx', ...] element that you are actually sorting. In this 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 be sorted in place, actually modifying your list. sorted (my_list, ...) will return a new sorted list.

+2
source

Works great !!! thanks for the help. Here is my story: I have a Turkish-Russian dictionary in xdxf format. the problem was sorting it. I found a solution here http://effbot.org/zone/element-sort.htm , but it did not sort Unicode characters. here is the final source code:

 #!/usr/bin/env python # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET import codecs alphabet = u"aâbcçdefgğhiıjklmnoöpqrstuüvwxyz" tree = ET.parse("dict.xml") # this element holds the phonebook entries container = tree.find("entries") data = [] for elem in container: keyd = elem.findtext("k") data.append([keyd, elem]) data.sort(key=lambda data: [alphabet.index(c) for c in data[0]]) container[:] = [item[-1] for item in data] tree.write("new-dict.xml", encoding="utf-8") 

sample content dict.xml

 <cont> <entries> <ar><k>â</k>def1</ar> <ar><k>a</k>def1</ar> <ar><k>g</k>def1</ar> <ar><k>w</k>def1</ar> <ar><k>n</k>def1</ar> <ar><k>u</k>def1</ar> <ar><k>ü</k>def1</ar> <ar><k>âb</k>def1</ar> <ar><k>ç</k>def1</ar> <ar><k>v</k>def1</ar> <ar><k>ac</k>def1</ar> </entries> </cont> 

Thank you all

0
source

Source: https://habr.com/ru/post/916056/


All Articles