排序 OrderedDict 不起作用
Sorting OrderedDict not working
我正在尝试对 ordereddict 进行排序,但没有成功。我尝试使用 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 的数字进行排序:
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])
我正在尝试对 ordereddict 进行排序,但没有成功。我尝试使用 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 的数字进行排序:
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])