Python对内部带有整数的字符串排序数组 [英] Python sort array of string with integers inside
本文介绍了Python对内部带有整数的字符串排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用python对列表格式进行排序
How i can use python to sort the list format
format=["12 sheet","4 sheet","48 sheet","6 sheet", "busrear", "phonebox","train"]
喜欢这种方式
format =["4 sheet", "6 sheet", "12 sheet", "48 sheet", "busrear, "phonebox", "train"]
如果数组是列表的列表,那么我们如何做到这一点
edit: If the array is a list of list then how can we do that like this one
format=[[1L, u'12 sheet', 0],[2L, u'4 sheet', 0], [3L, u'48 sheet', 0], [4L, u'6 sheet', 0 [5L, u'Busrear', 0], [6L, u'phonebox', 0], [7L, u'train', 0]]
推荐答案
>>> fmts =["12 sheet","4 sheet","48 sheet","6 sheet", "busrear", "phonebox","train"]
>>> fmts.sort(key=lambda x: (int(x.split(None, 1)[0]) if x[:1].isdigit() else 999, x))
>>> fmts
['4 sheet', '6 sheet', '12 sheet', '48 sheet', 'busrear', 'phonebox', 'train']
format
是内置函数.不要将其用作变量名.它将隐藏内置函数.
format
is a builtin function. Do not use it as a variable name. It will shadow the builtin function.
这篇关于Python对内部带有整数的字符串排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文