Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用插入排序对字典列表及其子项(python)的值进行排序_Python_Algorithm_Sorting - Fatal编程技术网

尝试使用插入排序对字典列表及其子项(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)
而(j
sortedScores=sorted([dictionary1,dictionary2,dictionary3],key=lambda x:int(x['timestamp')),reverse=True)
错误在“i['timestamp']”中 当为范围(1,len(…)中的i写入时,我变成了1和另一个数字之间的整数。因此i[timestamp]没有任何意义。请尝试更正:)