尝试使用插入排序对字典列表及其子项(python)的值进行排序
我的字典来自一个数据库,但对于这个例子,假设我有两个字典尝试使用插入排序对字典列表及其子项(python)的值进行排序,python,algorithm,sorting,Python,Algorithm,Sorting,我的字典来自一个数据库,但对于这个例子,假设我有两个字典 dictionary1 = { "score": str(percentageScore), #totalScore "date": str(today), #today "title": str(title), #title "timestamp": str(t
dictionary1 = {
"score": str(percentageScore), #totalScore
"date": str(today), #today
"title": str(title), #title
"timestamp": str(timestamp) #timestamp to display results in order
}
时间戳是一个整数,我想使用它对包含多个字典的python列表进行排序,如下面所示,按照时间戳整数的降序排列
我已尝试通过以下方式实现这一点:
sortedScores = [dictionary1, dictionary2, dictionary3]
for i in range(1, len(sortedScores)):
sortKey = sortedScores[i['timestamp']]
j = (i - 1)
while (j <= 0) and (sortKey < (sortedScores[j])['timestamp']):
sortedScores[j+1] = sortedScores[j]
j -= 1
sortedScores[j+1] = sortKey
sortedScores=[dictionary1、dictionary2、dictionary3]
对于范围内的i(1,len(分类的核心)):
sortKey=sortedScores[i['timestamp']]
j=(i-1)
而(jsortedScores=sorted([dictionary1,dictionary2,dictionary3],key=lambda x:int(x['timestamp')),reverse=True)
错误在“i['timestamp']”中
当为范围(1,len(…)中的i写入时,我变成了1和另一个数字之间的整数。因此i[timestamp]没有任何意义。请尝试更正:)