from collections import OrderedDict
a = {'score':3, 'color':'red'}
b = {'score':1, 'color':'yellow'}
lst = []
for d in [a,b]:
temp = OrderedDict()
for k in sorted(d, reverse=True):
temp.setdefault(k, d[k])
lst.append(temp)
OUTPUT: [OrderedDict([('score', 3), ('color', 'red')]), OrderedDict([('score', 1), ('color', 'yellow')])]
虽然项目的顺序并不重要,但是如果您真的希望有特定的顺序,可以使用OrderedDict。在
你应该把你的口述分类,然后把它们转换成有序的话
相关问题 更多 >
编程相关推荐