I am trying to sort a dictionary by list of lists. The items in the list of lists are keys in the dictionary. I asked it before but the answers didn't solve the issue.
My input list is:
mylist= [
['why', 'was', 'cinderella', 'late', 'for', 'the', 'ball', 'she', 'forgot', 'to', 'swing', 'the', 'bat'],
['why', 'is', 'the', 'little', 'duck', 'always', 'so', 'sad', 'because', 'he', 'always', 'sees', 'a', 'bill', 'in', 'front', 'of', 'his', 'face'],
['what', 'has', 'four', 'legs', 'and', 'goes', 'booo', 'a', 'cow', 'with', 'a', 'cold'],
['what', 'is', 'a', 'caterpillar', 'afraid', 'of', 'a', 'dogerpillar'],
['what', 'did', 'the', 'crop', 'say', 'to', 'the', 'farmer', 'why', 'are', 'you', 'always', 'picking', 'on', 'me']
]
My dictionary somewhat looks like this:
myDict = {'to': [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56],
'jam': [20], 'black': [5], 'farmer': [11],
'woodchuck': [54], 'has': [14, 16, 51], 'who': [16]
}
My code is:
def sort_by_increasing_order(mylist, myDict):
#temp = sorted(myDict, key=myDict.get)
temp = sorted(myDict, key=lambda tempKey: for tempKey in mylist, reverse=True )
return temp
Something like:
sort_by_increasing_order(['d', 'e', 'f'], {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]})
result: ['f', 'e', 'd']
So for my sample input it would look like:
sort_by_increasing_order(mylist, myDict)
>> ['to','woodchuck','has','jam','who','farmer']
The commented line just sorts by the dictionary keys when i try to sort by the list. My approach is not correct. The result should a list with increasing order of the length of indices as mentioned above. Any suggestion.