gpt4 book ai didi

python - 如何在 Python 中对节号列表进行排序?

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:44 25 4
gpt4 key购买 nike

<分区>

书籍章节通常编号为x.x.x,例如1.2.3如何对节号列表进行排序?

将节号存储为字符串列表。

# 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']

然而,我期望的结果是,

['1.1', '1.10', '1.2', '1.2.1', '1.2.3', '1.9']

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com