python 对末尾带有数字的字符串进行排序
对末尾带有数字的字符串列表进行排序(有些有 3 位数字,有些有 4 位数字)的最简单方法是什么:
>>> list = ['asdf123', 'asdf1234', 'asdf111', 'asdf124']
>>> list.sort()
>>> print list
['asdf111', 'asdf123', 'asdf1234', 'asdf124']
应该将 1234 放在最后。有没有简单的方法可以做到这一点?
what is the easiest way to sort a list of strings with digits at the end where some have 3 digits and some have 4:
>>> list = ['asdf123', 'asdf1234', 'asdf111', 'asdf124']
>>> list.sort()
>>> print list
['asdf111', 'asdf123', 'asdf1234', 'asdf124']
should put the 1234 one on the end. is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,
您可以使用 natsort 模块。
完全公开,我是该包的作者。
Yes
You can use the natsort module.
Full disclosure, I am the package's author.
不,
完全不清楚真正的规则是什么。 “有些有 3 位数字,有些有 4 位数字”实际上并不是一个非常精确或完整的规范。您的所有示例都在数字前面显示 4 个字母。这总是正确的吗?
该
key
函数可能会执行您想要的操作。或者它可能太复杂了。或者也许模式确实是r"^(.*)(\d{3,4})$"
或者规则可能更加模糊。No
It's perfectly unclear what the real rules are. The "some have 3 digits and some have 4" isn't really a very precise or complete specification. All your examples show 4 letters in front of the digits. Is this always true?
That
key
function might do what you want. Or it might be too complex. Or maybe the pattern is reallyr"^(.*)(\d{3,4})$"
or maybe the rules are even more obscure.您所描述的可能称为 自然排序,或人类排序。如果您使用的是 Python,则可以借鉴 Ned 的实现。
自然排序的算法大致如下:
What you're probably describing is called a Natural Sort, or a Human Sort. If you're using Python, you can borrow from Ned's implementation.
The algorithm for a natural sort is approximately as follows:
你需要一个关键功能。您愿意在最后指定 3 或 4 位数字,并且我有一种感觉您希望它们进行数字比较。
如果没有 lambda 和条件表达式,
这只是利用了元组按第一个元素排序,然后是第二个元素排序的事实。因此,由于调用了 key 函数来获取要比较的值,因此现在将像 key 函数返回的元组一样对元素进行比较。例如,
'asdfbad123'
将与'asd7890'
进行比较,就像('asdfbad', 123)
与('asd' 进行比较一样,7890)
。如果字符串的最后 3 个字符实际上不是数字,您将得到一个 ValueError ,鉴于您向其传递的数据不符合其设计规范,因此这是完全合适的。You need a key function. You're willing to specify 3 or 4 digits at the end and I have a feeling that you want them to compare numerically.
Without the lambda and conditional expression that's
This just takes advantage of the fact that tuples sort by the first element, then the second. So because the
key
function is called to get a value to compare, the elements will now be compared like the tuples returned by the key function. For example,'asdfbad123'
will compare to'asd7890'
as('asdfbad', 123)
compares to('asd', 7890)
. If the last 3 characters of a string aren't in fact digits, you'll get a ValueError which is perfectly appropriate given the fact that you passed it data that doesn't fit the specs it was designed for.问题是这里的排序是按字母顺序排列的,因为它们是字符串。在移动到下一个字符之前,会比较每个字符序列。
您需要进行数字排序才能获得所需的输出。
The issue is that the sorting is alphabetical here since they are strings. Each sequence of character is compared before moving to next character.
You will need to due numeric sorting to get the desired output.
我没有自己拆分每一行,而是要求 python 使用
re.findall()
为我完成此操作:rather than splitting each line myself, I ask python to do it for me with
re.findall()
: