Python:按数字对列表中的列表进行排序

标签 python sorting

我有一个这样的列表:

list1 = [['art_25', 'title', 'author'], ['art_12', 'title', 'author'], ['art_4', 'title', 'author']]

我如何排序这个列表 s.t.输出等于

[['art_4', 'title', 'author'], ['art_12' ...], ['art_25', ...]] ?

感谢您的帮助!

最佳答案

您可以将 sortedkey 参数一起使用。您可以创建一个 lambda 表达式,在 '_' 字符上拆分,将其转换为 int,然后按该值排序。

list1 = [['art_25', 'title', 'author'],
         ['art_12', 'title', 'author'],
         ['art_4', 'title', 'author']]

>>> sorted(list1, key=lambda i: int(i[0].split('_')[1]))
[['art_4', 'title', 'author'],
 ['art_12', 'title', 'author'],
 ['art_25', 'title', 'author']]

关于Python:按数字对列表中的列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26280984/

相关文章:

java - 自定义 Java 按名称排序

java - 在 SWT/Eclipse RCP 中对基本树进行排序

python - 使用 python,除一个元素外,按字母顺序对 XML 进行排序

sql - 如何对版本号进行排序,例如 ‘A’ 、 ‘B’ 、 ‘AA’ 、 ‘AB’ 和 ‘AAA’ ?

python - 使用 Surprise SVD++ 算法获取所有用户的预测

python - 有效地列出给定 Unicode 类别中的所有字符

python - 为什么在 Windows 10 下启动 python 如此缓慢,以及如何加快速度

python - 在同一个图上使用 Matplotlib 和 Pandas 绘制图

python - 从批处理文件运行 python 脚本时出现 ModuleNotFoundError

java - 一维排序数组,但 array[i+1] 始终打印索引零处的元素