0

This is the function to read words from text file, sort these and then store in another text file.

#file contains words
file=open('/content/gdrive/MyDrive/Post_OCR_Classifictaion/Dict_try.txt').read().split()

#sorting order based on letters
letters="abcçdefgğhıijklmnoöprsştuüvyz"
d={i:letters.index(i) for i in letters}

#sort function
sorted_list=sorted(file,key=d.get)

#store after sortting in new file
textfile = open("/content/gdrive/MyDrive/Post_OCR_Classifictaion/Dict_try_sort.txt", "w")
for element in sorted_list:
    textfile.write(element + "\n")
textfile.close()

These are the words in text file:

aço

çzb

ogğ

beg

zğe

öge

ğg

gaço

ogğ

But it gives error:

enter image description here

maryam mehboob
  • 338
  • 5
  • 17
  • seems dictionary doesn't have values, throws none which cant be given to sort function Please use filter to filter all Nones in list – Naga kiran Aug 04 '21 at 14:00
  • It do sort if there are just letters in text file but for words it is throwing this error. – maryam mehboob Aug 04 '21 at 14:03
  • Your dict is consisted of one-letter strings. Not a single word will be in that dict so `get` will return `None`. Why do you need a `key` for the sort? That's already the default... – Tomerikoo Aug 04 '21 at 14:04
  • Does this answer your question? [Sorting string values according to a custom alphabet in Python](https://stackoverflow.com/questions/26579392/sorting-string-values-according-to-a-custom-alphabet-in-python) – Tomerikoo Aug 04 '21 at 14:07
  • Be careful: `Dict_try.txt` is not closed. – qouify Aug 04 '21 at 14:10
  • @qouify See [Is explicitly closing files important?](https://stackoverflow.com/q/7395542/6045800) for more details and refinement of my comment – Tomerikoo Aug 04 '21 at 14:15

3 Answers3

3

Here

sorted_list=sorted(file,key=d.get)

file is list of words whilst d is dict with keys being letters. You need first retrieve first letter of word then search for it in dict, for example using lambda i.e.

sorted_list=sorted(file,key=lambda word:d[word[0]])
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • 1
    That will not sort the words starting with the same letter – Tomerikoo Aug 04 '21 at 14:08
  • This worked for me but when I used it for more words file it is giving KeyError: '\u202a'. why is this? – maryam mehboob Aug 04 '21 at 14:10
  • @maryammehboob Because you have characters in the file that are not in the dict... Please note that the above code will not inter-sort words starting with the same letter... – Tomerikoo Aug 04 '21 at 14:13
0

You can convert your words to indexes:

def key(word):
    return tuple(d[x] for x in word)
sorted_list = sorted(file, key=key)
qouify
  • 3,698
  • 2
  • 15
  • 26
0

You should probably try to account for both upper and lower case and also any circumstance where the line in 'file' either has a length of zero or starts with a character that is not in your list. Consider this possible solution:-

letters = "abcçdefgğhıijklmnoöprsştuüvyz"
letters = letters.upper() + letters

def myfunc(s):
    try:
        return letters.index(s[0])
    except Exception:
        pass
    return -1


ml = ['ğc', 'öp', 'Rr', 'Po', 'aw', 'tp', 'çd']
mls = sorted(ml, key=myfunc)
print(mls)

The output of this would be:-

['Po', 'Rr', 'aw', 'çd', 'ğc', 'öp', 'tp']