本文介绍了如何在Python中对压缩列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用Python的方式对压缩列表进行排序的方式是什么?
What's the Pythonic way to sort a zipped list?
代码:
names = list('datx')
vals = reversed(list(xrange(len(names))))
zipped = zip(names, vals)
print zipped
上面的代码显示 [('d',3),('a',2),('t',1),('x',0)]
我想按值对压缩进行排序.因此理想情况下,它最终看起来应该像这样 [('x',0),('t',1),('a',2),('d',3)] .
I want to sort zipped by the values. So ideally it would end up looking like this [('x', 0), ('t', 1), ('a', 2), ('d', 3)].
推荐答案
zipped.sort(key = lambda t: t[1])
这篇关于如何在Python中对压缩列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!