python中按子字符串数值对字符串排序
我有一个字符串列表,需要使用两个子字符串作为int键按数字顺序排序。 显然,使用python中按子字符串数值对字符串排序,python,string,sorting,Python,String,Sorting,我有一个字符串列表,需要使用两个子字符串作为int键按数字顺序排序。 显然,使用sort()函数按字母顺序对字符串排序,因此得到1,10,2。。。这显然不是我想要的 四处搜索,我发现一个键参数可以传递给sort()函数,使用sort(key=int)应该可以做到这一点,但作为我的键,子字符串而不是整个字符串应该会导致强制转换错误 假设我的字符串类似于: test1txtfgf10 test1txtfgg2 test2txffdt3 test2txtsdsd1 我希望我的列表在第一个整数的基础上
sort()
函数按字母顺序对字符串排序,因此得到1,10,2。。。这显然不是我想要的
四处搜索,我发现一个键参数可以传递给sort()
函数,使用sort(key=int)
应该可以做到这一点,但作为我的键,子字符串而不是整个字符串应该会导致强制转换错误
假设我的字符串类似于:
test1txtfgf10
test1txtfgg2
test2txffdt3
test2txtsdsd1
我希望我的列表在第一个整数的基础上按数字顺序排列,然后在第二个整数的基础上按数字顺序排列,因此我会:
test1txtfgg2
test1txtfgf10
test2txtsdsd1
test2txffdt3
我想我可以提取整数值,只对它们进行排序,跟踪它们所属的字符串,然后对字符串进行排序,但我想知道是否有一种方法可以更高效、更优雅地完成这项工作
提前感谢尝试以下方法
In [26]: import re
In [27]: f = lambda x: [int(x) for x in re.findall(r'\d+', x)]
In [28]: sorted(strings, key=f)
Out[28]: ['test1txtfgg2', 'test1txtfgf10', 'test2txtsdsd1', 'test2txffdt3']
这将使用正则表达式(the)查找每个字符串中的所有整数,然后。例如,f('test1txtfgg2')
返回[1,2]
,然后将其与其他列表进行比较。尝试以下操作
In [26]: import re
In [27]: f = lambda x: [int(x) for x in re.findall(r'\d+', x)]
In [28]: sorted(strings, key=f)
Out[28]: ['test1txtfgg2', 'test1txtfgf10', 'test2txtsdsd1', 'test2txffdt3']
这将使用正则表达式(the)查找每个字符串中的所有整数,然后。例如,
f('test1txtfgg2')
返回[1,2]
,然后将其与其他列表进行比较。提取数字部分并使用它们进行排序
import re
d = """test1txtfgf10
test1txtfgg2
test2txffdt3
test2txtsdsd1"""
lines = d.split("\n")
re_numeric = re.compile("^[^\d]+(\d+)[^\d]+(\d+)$")
def key(line):
"""Returns a tuple (n1, n2) of the numeric parts of line."""
m = re_numeric.match(line)
if m:
return (int(m.groups(1)), int(m.groups(2)))
else:
return None
lines.sort(key=key)
现在,行
是
['test1txtfgg2', 'test1txtfgf10', 'test2txtsdsd1', 'test2txffdt3']
提取数字部分并使用它们进行排序
import re
d = """test1txtfgf10
test1txtfgg2
test2txffdt3
test2txtsdsd1"""
lines = d.split("\n")
re_numeric = re.compile("^[^\d]+(\d+)[^\d]+(\d+)$")
def key(line):
"""Returns a tuple (n1, n2) of the numeric parts of line."""
m = re_numeric.match(line)
if m:
return (int(m.groups(1)), int(m.groups(2)))
else:
return None
lines.sort(key=key)
现在,行
是
['test1txtfgg2', 'test1txtfgf10', 'test2txtsdsd1', 'test2txffdt3']
Python 2还是3?我问这个问题的原因是Python2有一个
cmp
参数。虽然您可以自己编写这个参数,但PyPI上有一些非常好的“自然排序”库,您可能想看看。除了更简单之外,他们可能还想到了您没有想到的边缘情况,或者以您不会费心的方式优化了一些东西,等等。Python 2或3?我问这个问题的原因是Python2有一个cmp
参数。虽然您可以自己编写这个参数,但PyPI上有一些非常好的“自然排序”库,您可能想看看。除了更容易,他们可能还想到了你没有想到的边缘情况,或者以你不会费心的方式优化了事情,等等。