This question already has an answer here:
这个问题在这里已有答案:
- Sorting a list of dot-separated numbers, like software versions 4 answers
- 排序点分隔数字列表,如软件版本4答案
Book sections are usually numbered as x.x.x
, such as 1.2.3
. How do I sort a list of section numbers?
书籍部分通常编号为x.x.x,例如1.2.3。如何对节号列表进行排序?
Store section numbers as a list of strings.
将部分编号存储为字符串列表。
# a list of strings, section numbers
ls = ['1.1', '1.10', '1.2', '1.2.3', '1.2.1', '1.9']
lists = sorted([s.split('.') for s in ls], key=lambda x:map(int, x))
# [['1', '1'], ['1', '2'], ['1', '2', '1'], ['1', '2', '3'], ['1', '9'], ['1', '10']]
r = ['.'.join(sublist) for sublist in lists]
#['1.1', '1.2', '1.2.1', '1.2.3', '1.9', '1.10']
However, my expecting result is,
但是,我期待的结果是,
['1.1', '1.10', '1.2', '1.2.1', '1.2.3', '1.9']
3 个解决方案
#1
7
Use a custom compare function that converts the strings into sub-lists of integers. Those will sort correctly without problems.
使用自定义比较函数将字符串转换为整数的子列表。那些将正确排序没有问题。
In [4]: ls = ['1.1', '1.10', '1.2', '1.2.3', '1.2.1', '1.9']
In [5]: def section(s):
...: return [int(_) for _ in s.split(".")]
...:
In [6]: sorted(ls, key=section)
Out[6]: ['1.1', '1.2', '1.2.1', '1.2.3', '1.9', '1.10']
#2
4
As by your comments, float
is not the datatype you need. In your case, you have an actual hierarchy of chapters/sections.
正如您的评论所示,float不是您需要的数据类型。在您的情况下,您有章节/部分的实际层次结构。
One simple (and remember, simple is better than complex) way is to represent the section numbers as tuples. Since tuples are sorted lexicographically, they naturally sort in the desired order:
一个简单的(记住,简单比复杂更好)方法是将节号表示为元组。由于元组按字典顺序排序,因此它们按所需顺序自然排序:
>>> lf = [(1, ), (1, 1), (1, 10), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, ), (1, 9)]
>>> sorted(lf)
[(1, ), (1, 1), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, )]
As we can see, this also works for tuples with varying lengths.
我们可以看到,这也适用于不同长度的元组。
If want to keep the sections as strings, natsort
does a fine job of handling dotted values, too:
如果想将这些部分保留为字符串,natsort也可以很好地处理虚线值:
>>> s = ['1', '1.1', '1.10', '1.2']
>>> natsort.natsorted(s)
['1', '1.1', '1.2', '1.10']
You can also define your own SectionNumber
class, but that's probably overkill.
您也可以定义自己的SectionNumber类,但这可能是过度的。
#3
3
Book sections are usually numbered as x.x.x
书籍部分通常编号为x.x.x.
Why not store the section numbers as tuples?
为什么不将节号存储为元组?
sections = [(2, 4, 1), (1, 10, 3),(1, 2, 1), (1, 1, 10), (1, 2, 3), (1, 4, 6)]
print(sorted(sections))
gives [(1, 1, 10), (1, 2, 1), (1, 2, 3), (1, 4, 6), (1, 10, 3), (2, 4, 1)]
给出[(1,1,10),(1,2,1),(1,2,3),(1,4,6),(1,10,3),(2,4,1)]
#1
7
Use a custom compare function that converts the strings into sub-lists of integers. Those will sort correctly without problems.
使用自定义比较函数将字符串转换为整数的子列表。那些将正确排序没有问题。
In [4]: ls = ['1.1', '1.10', '1.2', '1.2.3', '1.2.1', '1.9']
In [5]: def section(s):
...: return [int(_) for _ in s.split(".")]
...:
In [6]: sorted(ls, key=section)
Out[6]: ['1.1', '1.2', '1.2.1', '1.2.3', '1.9', '1.10']
#2
4
As by your comments, float
is not the datatype you need. In your case, you have an actual hierarchy of chapters/sections.
正如您的评论所示,float不是您需要的数据类型。在您的情况下,您有章节/部分的实际层次结构。
One simple (and remember, simple is better than complex) way is to represent the section numbers as tuples. Since tuples are sorted lexicographically, they naturally sort in the desired order:
一个简单的(记住,简单比复杂更好)方法是将节号表示为元组。由于元组按字典顺序排序,因此它们按所需顺序自然排序:
>>> lf = [(1, ), (1, 1), (1, 10), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, ), (1, 9)]
>>> sorted(lf)
[(1, ), (1, 1), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, )]
As we can see, this also works for tuples with varying lengths.
我们可以看到,这也适用于不同长度的元组。
If want to keep the sections as strings, natsort
does a fine job of handling dotted values, too:
如果想将这些部分保留为字符串,natsort也可以很好地处理虚线值:
>>> s = ['1', '1.1', '1.10', '1.2']
>>> natsort.natsorted(s)
['1', '1.1', '1.2', '1.10']
You can also define your own SectionNumber
class, but that's probably overkill.
您也可以定义自己的SectionNumber类,但这可能是过度的。
#3
3
Book sections are usually numbered as x.x.x
书籍部分通常编号为x.x.x.
Why not store the section numbers as tuples?
为什么不将节号存储为元组?
sections = [(2, 4, 1), (1, 10, 3),(1, 2, 1), (1, 1, 10), (1, 2, 3), (1, 4, 6)]
print(sorted(sections))
gives [(1, 1, 10), (1, 2, 1), (1, 2, 3), (1, 4, 6), (1, 10, 3), (2, 4, 1)]
给出[(1,1,10),(1,2,1),(1,2,3),(1,4,6),(1,10,3),(2,4,1)]