我想按列表列表排序字典。列表 列表中的项目是字典中的键。按升序排列列表中的字典
喜欢的东西:
sort_by_increasing_order(['d', 'e', 'f'], {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]})
result: ['f', 'e', 'd']
我的输入列表是:
[
['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']
]
我的字典里有些看起来是这样的:
{'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]
}
我有什么打算是
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
输出将是这样的:
sort_by_increasing_order(mylist, myDict)
>> ['to','woodchuck','has','jam','who','farmer','black']
的注释行刚刚通过排序的字典键,当我试图通过列表进行排序。 我的方法不正确。结果应该是如上所述的具有指数长度 的递增顺序的列表。任何想法
增加什么样的顺序?为什么在你的例子中'f'首先? – user2357112
我更新了样本字典。字典中索引长度的增加顺序等等是否澄清? – user3247054
@ user2357112有什么建议吗? – user3247054