python - 在 python 3 中排序列表

标签 python list defaultdict

因此,我需要根据第二个嵌套列表的第一个 元素(包含 01, 02, 03 等元素,对其中包含一些随机数据的列表进行排序,等):

  [['00553', ['01', '3.4']], ['00553', ['02', '2.1']], ['00551', ['02', '5.3']], etc]

此随机数据稍后在 defaultdict 中与其他一些数据一起使用,以便将其组合在一起并通过键打印出来(键是数字,如 00553、00551)。

我试图在将它放入 defaultdict 之前对其进行排序,但我得到的只是嵌套列表本身的值..

谁能帮帮我,我是新手。

最佳答案

>>> lis = [['00553', ['01', '3.4']], ['00553', ['02', '2.1']], ['00551', ['02', '5.3']]]
>>> sorted(lis, key = lambda x: int(x[1][0])) 
[['00553', ['01', '3.4']], ['00553', ['02', '2.1']], ['00551', ['02', '5.3']]]

关于python - 在 python 3 中排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16877060/

相关文章:

python - .one() 和 .scalar() 有什么区别

python - 如何中断 SimPy 中的嵌套进程?

python - 重命名模板中对 Django 消息的引用?

javascript - 如果我添加 alert(),为什么我的 JavaScript 函数执行不确定?

python - DefaultDict ,在附加元素上,维护按添加顺序排序的键

使用 defaultdict 的 Python 单行树。如何减少所需参数的数量?

python - DefaultDict 的替代品

python - 在 Swift 中导入 Python 代码和库

python - 我正在做很多列表和字典排序......这导致 Python 网站出现内存错误

c# - 如何以编程方式列出已安装的视频编解码器?