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

编程入门 行业动态 更新时间:2024-10-27 06:31:32
本文介绍了Python按字符串中的第一个数字对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

["10. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 I","11. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 J","12. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 K","13. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 L","14. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 M","15. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 N","16. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 O","17. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 P","18. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 Q","19. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 R","20. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 S","21. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 z","22. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 A",5. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 D","6. 命令安装补充条 - 大包 1 6 条 E","7. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 F",8. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 G",9. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 H"]

这是我目前尝试过的代码.

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

但这又回来了;

["10. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 I","11. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 J","12. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 K","13. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 L","14. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 M","15. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 N","16. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 O","17. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 P","18. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 Q","19. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 R","20. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 S","21. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 z","22. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 A","5. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 D","6. 命令安装补充条 - 大包 1 6 条 E","7. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 F",8. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 G",9. iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 H"]

更新

我也可以举个例子,在没有 .特点.例如;20 iTOMA 收音机闹钟 FM 数字收音机时钟床头闹钟无线 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]))

使用或不使用 '.'.

I would like to sort this list of strings by the first number preferably using using regular expressions in a single line but other suggestions are welcome. I am trying to get the quickest way of doing it. Here is the list;

[ "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" ]

This is the code that I have tried so far.

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

But this is returning;

[ "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" ]

Update

Can I also have an example of sorting it without having the . character. For example; "20 iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless S"

解决方案

Convert to int after splitting on . to take the full numbers, not just the first digit:

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

To sort when the '.' are not certain to be in the strings:

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

Works with or without the '.'.

更多推荐

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

本文发布于:2023-11-30 10:05:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数字   列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!