Search code examples
pythonsorting

How do I sort a list of words in alphabetical order from a custom alphabet?


I'm trying to make a language and to alphabetically sort the words in it, I'm making a python script for it because I can't make things easy for myself. I need to sort it with the alphabet: "æǽaàbdyýeéfgiíʒklmnŋΩΏoóøǿɤprʃsþðtʊuúʌvɯjz".

I tried the top answer in this post

def sortA(l, alphabet="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"):
    returnVal = sorted(l, key=lambda word: [alphabet.index(c) for c in word])
    return returnVal


keys = sortA(keys, "æǽaàbdyýeéfgiíʒklmnŋΩΏoóøǿɤprʃsþðtʊuúʌvɯjz")

and vsc says

Exception has occurred: ValueError
substring not found
  File "filepath", line 13, in <lambda>
    returnVal = sorted(l, key=lambda word: [alphabet.index(c) for c in word])
                                            ^^^^^^^^^^^^^^^^^

EDIT

The issue is probably an issue with the encoding of certain characters.

EDIT 2

I belive the issue is from here:

def newEntry():
    word = input("What is the word?: \n")
    defin = input("What is the definition of the word?: \n")
    etymology = input("Where does the word come from?:\n")
    detail = [defin, etymology]
    return {word: detail}

def entryToDict(e):
    spl = str(e).split("\n")
    return [spl[0], [spl[1],spl[2]]]



Read = (f.read()).split("\n\n")
dic = newEntry()
for i in Read:
    entry = entryToDict(i)
    dic[entry[0]] = entry[1]
keys = list(dic.keys())
f.close()

Solution

  • The problem was not in the code provided. It was an issue with reading the text file. Actual Problem