如何正确地sorting一个数字string里面?
可能重复:
Python是否有一个内置的string自然sorting函数?
我有一个包含数字的string列表,我找不到sorting它们的好方法。
例如,我得到这样的东西:
something1 something12 something17 something2 something25 something29
用sort()
方法。
我知道我可能需要以某种方式提取数字,然后对列表进行sorting,但是我不知道如何以最简单的方式进行sorting。
也许你正在寻找人类sorting (也称为自然sorting ):
import re def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments) ''' return [ atoi(c) for c in re.split('(\d+)', text) ] alist=[ "something1", "something12", "something17", "something2", "something25", "something29"] alist.sort(key=natural_keys) print(alist)
产量
['something1', 'something2', 'something12', 'something17', 'something25', 'something29']
PS。 我改变了我的答案,使用Toothy的自然sorting实现(张贴在这里的评论),因为它比我原来的答案快得多。
如果你想使用浮点数来sorting文本,那么你需要将匹配整数的正则expression式(即(\d+)
)更改为与浮点数匹配的正则expression式 :
import re def atof(text): try: retval = float(text) except ValueError: retval = text return retval def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments) float regex comes from https://stackoverflow.com/a/12643073/190597 ''' return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ] alist=[ "something1", "something2", "something1.0", "something1.25", "something1.105"] alist.sort(key=natural_keys) print(alist)
产量
['something1', 'something1.0', 'something1.105', 'something1.25', 'something2']