I have the following list:
listofanimals = ['Dog', 'Cat', 'Frog', 'Tiger', 'Sheep', 'Lion']
I would like to sort this list according to a dictionary:
score_card = {0: 'Sheep', 1: 'Dog', 2: 'Cat', 3: 'Tiger', 4: 'Frog', 5: 'Lion'}
Where the order of the final list has to be from 0 to 5, i.e.
numberorder = [0, 1, 2, 3, 4, 5]
I would like to create this inside a function. This is what I have:
def sorter():
listofanimals = ['Dog', 'Cat', 'Frog', 'Tiger', 'Sheep', 'Lion']
score_card = {0: 'Sheep', 1: 'Dog', 2: 'Cat', 3: 'Tiger', 4: 'Frog', 5: 'Lion'}
numbers = [0, 1, 2, 3, 4, 5]
finallist = []
for i in listofanimals:
print(numbers[listofanimals.index(i)])
q.append(numbers[listofanimals.index(i)])
print(q)
q = sorted(q)
finallist = [score_card[j] for j in q]
return finallist
I could not figure out how to convert listofanimals into numbers, so then I could sort the numbers and then feed back into the dictionary to get the values but I just cannot get it to work. Any help is appreciated.