Python按字符串中的第一个数字对列表进行排序 [英] Python sort list by first number in a string

查看:43
本文介绍了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按字符串中的第一个数字对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆