gpt4 book ai didi

Python 按字符串中的第一个数字对列表进行排序

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

我想按第一个数字对这个字符串列表进行排序,最好在一行中使用正则表达式,但欢迎提出其他建议。我正在尝试以最快的方式做到这一点。这是列表;

[
"10. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless I",
"11. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless J",
"12. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless K",
"13. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless L",
"14. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless M",
"15. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless N",
"16. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless O",
"17. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless P",
"18. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless Q",
"19. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless R",
"20. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless S",
"21. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless z",
"22. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless A",
"5. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless D",
"6. Command Mounting Refill Strips - Large Pack of 1 6 Strips E",
"7. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless F",
"8. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless G",
"9. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless H"
]

这是我到目前为止尝试过的代码。

dirs = sorted(next(walk(self.rootDirectory))[1], key=lambda x: int(x[0]))

但是这是返回;

[
"10. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless I",
"11. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless J",
"12. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless K",
"13. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless L",
"14. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless M",
"15. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless N",
"16. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless O",
"17. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless P",
"18. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless Q",
"19. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless R",
"20. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless S",
"21. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless z",
"22. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless A",
"5. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless D",
"6. Command Mounting Refill Strips - Large Pack of 1 6 Strips E",
"7. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless F",
"8. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless G",
"9. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless H"
]

更新

我也可以举一个没有 .特点。例如; “20 iTOMA radio 闹钟 FM 数字 radio 时钟床边闹钟无线 S”

最佳答案

. 拆分后转换为 int 以获取完整数字,而不仅仅是第一个数字:

lst = next(walk(self.rootDirectory))[1]
dirs = sorted(lst, key=lambda x: int(x.split('.')[0]))

'.' 不确定在字符串中时进行排序:

dirs = sorted(lst, key=lambda x: float(x.split()[0]))

使用或不使用 '.'

关于Python 按字符串中的第一个数字对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46772801/

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