1

I have a two dimensional list and I want to sort them priority. my list like; [0] is number, [1] is name-surname, [2] is degree .. etc And the names are include non-english characters like "Ç İ Ş" and sort/sorted method doesnt work. Thats why I implemented a alphabet like; alphabet = "abcçdefgğhıijklmnoöprsştuüvyz" and I have to sort them like following alphabet.

here is my code;

list.sort(key=lambda list : list[0]) -< this is first priority about number.
list.sort(key=lambda list : list[1]) -< this is second priority about name and surname

but this doesnt work and ı dont even know how to sort them with alphabetically in my alphabet. any help?

How to sort a list with an exception in Python this link is also an answer for this question but my list is not tuple, so doesnt work. Can anyone help me ?

maddog
  • 29
  • 4
  • The first one has a typo ```lambda``` instead of ```labmda```. You can try this: ```list.sort(key=labmda x : [x[0],x[1]])``` –  Aug 07 '21 at 17:53
  • 1
    Additional Note: Don't name your lists `list` because that is a python keyword and will make it messy if you define `list()` objects. – pu239 Aug 07 '21 at 17:56
  • Please show a sample of the list(s) you're trying to sort and the expected/required output –  Aug 07 '21 at 17:57
  • 2
    @Sujay you made the same typo . Nice! – pu239 Aug 07 '21 at 17:58
  • In the answer that you linked yourself, there are no tuples involved in the list. The `tuple()` being made is temporary and for sorting, even in the answer, they have a list, not a tuple. – pu239 Aug 07 '21 at 18:03
  • For example I have a name "çetin" letter "ç" which comes after letter "c". On this point ç should comes after c but sort methods doesnt work on it. Thats the problem ı should fix about this. I need an alphabet ı guess. How can i make letter "ç" comes after letter "c" without alphabet? – maddog Aug 07 '21 at 18:05

2 Answers2

0

I am sure that there is a better way to do this but you can use dictionaries to bind each of your character in your alphabet to a number. You can use that dict as a key in the sorted() function, so that it sorts according to your needs.

>>letters="abcçdefgğhıijklmnoöprsştuüvyz"
>>d={i:letters.index(i) for i in letters}
>>sorted("açobzöğge", key=d.get)
['a', 'b', 'ç', 'e', 'g', 'ğ', 'o', 'ö', 'z']

The idea and the code snippet is not mine, check out this answer to basically the same question: https://stackoverflow.com/a/6057052

Anon
  • 1
0

there is a typo in the first line. Also, if you sort according to 2nd priority after 1st priority, then sorting according to 1st priority nullifies. So it is better to applying sorting according to 1st priority later. This code should work

sorted(words, key=lambda l : l[1])
sorted(words, key=lambda l : l[0])
print(list)
Srishti Ahuja
  • 181
  • 1
  • 7