排序OrderedDict不起作用

时间:2016-08-05 19:19:16

标签: python sorting dictionary ordereddictionary

我试图对有序的人进行排序,但没有运气。我尝试使用https://docs.python.org/2/library/collections.html#ordereddict-examples-and-recipes

中找到的方法
        for d in results:
            print(d)

        d = OrderedDict(results)

        for key in d:
            print(d[key])

        hi = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

        for key in hi:
            print(hi[key])

打印出来:

('GOOG/CPH_43740', -1.6749609142444124)
('GOOG/CPH_CARL_B', 0.66869383328307086)
('GOOG/CPH_CHR', 0.6270772411141563)
('GOOG/CPH_EGE_B', -0.89015989892984237)
('GOOG/CPH_GEN', 0.46485175444884497)

-1.67496091424
0.668693833283
0.627077241114
-0.89015989893
0.464851754449

-1.67496091424
0.668693833283
0.627077241114
-0.89015989893
0.464851754449

未按降序排序。我完全迷失了吗?

1 个答案:

答案 0 :(得分:3)

假设你想对你的元组中包含索引1而不是0的数字进行排序:

    for d in results:
        print(d)

    d = OrderedDict(results)

    for key in d:
        print(d[key])

    hi = OrderedDict(sorted(d.items(), key=lambda t: t[1]))

    for key in hi:
        print(hi[key])