简体   繁体   English

如何在Python中对带有结尾零的数字字符串进行排序?

[英]How to sort numeric strings with trailing zeroes in Python?

I want to sort a list of numeric strings (w/ trailing zeroes) like how you normally sort number. 我想对数字字符串列表(带尾随零)进行排序,就像通​​常对数字进行排序一样。 I'd also like to return a True bool if a provided numeric string is in that list. 如果该列表中提供了数字字符串,我还想返回True bool。

Example: 例:

old_list = [1.1, 1.8, 1.50, 1.5, 1.9, 2.1]

to

sorted = [1.1, 1.5, 1.8, 1.9, 1.50, 2.1]

So, obviously the decimals are treated like numbers and sorted like numbers. 因此,显然小数被视为数字,并被视为数字。 1.50 should not be simplified as 1.5, etc. It is used for product id for my company and has been bothering me for a year already. 1.50不应简化为1.5等。它用于我公司的产品ID,已经困扰了我一年。

I have tried converting the numeric strings into str then sort. 我尝试将数字字符串转换为str然后进行排序。 I also tried using the Decimal module but it turns 1.1 into sth like 1.10000000000000008881... 我也尝试使用Decimal模块,但是它将1.1变成1.10000000000000008881 ...

Here is the code I used: 这是我使用的代码:

object_num = list(map(str, ['1.1', '1.8', '1.50', '1.5', '1.9', '2.1']))
object_num.sort()
print(object_num)

Result: 结果:

['1.1', '1.5', '1.50', '1.8', '1.9', '2.1']

I appreciate that Python is smart enough to sort the list numerically, but I hope it could also get me this result: 我感谢Python足够聪明,可以按数字对列表进行排序,但是我希望它也可以使我得到以下结果:

sorted = [1.1, 1.5, 1.8, 1.9, 1.50, 2.1]

And when I typed 当我键入

print(1.50 in sorted)

It could return True. 它可以返回True。

Edit1: sorry for not making myself clear. Edit1:对不起,让我不清楚。 Let me explain the numbering system my company is using: 让我解释一下我公司正在使用的编号系统:

ID for first product from series A: 1.1 系列A的第一个产品的ID:1.1

ID for second product from series A: 1.2 A系列第二个产品的ID:1.2

... ...

ID for fiftieth product from series A: 1.50 A系列第五十个产品的ID:1.50

ID for first product from series B: 2.1 B系列首个产品的ID:2.1

All I want is to sort products from series A first, then series B 我只想对A系列的产品进行分类,然后对B系列的产品进行分类

Thus, 1.5 is indeed different from 1.50 in my case 因此,在我看来,1.5确实不同于1.50

One solution using re module: 一种使用re模块的解决方案:

import re

lst = ['1.1', '1.8', '1.50', '1.5', '1.9', '2.1']

l = sorted(lst, key=lambda k: [*map(int, re.findall(r'\d+', k))])
print(l)

Prints: 打印:

['1.1', '1.5', '1.8', '1.9', '1.50', '2.1']

Or without re : 还是不用re

l = sorted(lst, key=lambda k: [*map(int, k.split('.'))])
print(l)

For version numbers you could use the (unfortunately not so well documented) distutils.version module: 对于版本号,您可以使用distutils.version模块(不幸的是,文献记录不多):

from distutils.version import LooseVersion  # or StrictVersion

object_num = ['1.1', '1.8', '1.50', '1.5', '1.9', '2.1']
object_num.sort(key=LooseVersion)
print(object_num)

Result: 结果:

['1.1', '1.5', '1.8', '1.9', '1.50', '2.1']

It will also handle version numbers like "1.5.2b2" (eg for beta versions or release candidates). 它还将处理“ 1.5.2b2”之类的版本号(例如,针对Beta版本或发行候选版本)。 Depending on your needs you can choose between LooseVersion or StrictVersion (see the docstrings for the differences between them). 根据您的需要,您可以在LooseVersionStrictVersion之间进行选择(有关它们之间的区别,请参见文档字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM