Python排序列表中的第一个数字

时间:2017-10-16 14:33:14

标签: python python-3.x

我想用第一个数字对这个字符串列表进行排序,最好是在一行中使用正则表达式,但欢迎提出其他建议。我正在努力获得最快捷的方式。这是清单;

[
  "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 Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless S"

4 个答案:

答案 0 :(得分:10)

.分割后转换为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]))

使用或不使用'.'

答案 1 :(得分:3)

如果您希望通过数字字符串对列表进行排序,请考虑natural sorting algorithm

<强>代码

import re


def natural_key(string_):
    return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_) if s]

后一个代码是从这个SO post修改的,并假设每个字符串都被枚举,以便数字字符串转换为可以是sorted的数字。

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

sorted(iterable, key=natural_key)

输出

['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',
 '10. iTOMA Radio Alarm Clock FM Digital Radio Clock Bedside Alarm Clock Wireless I',
 ...]

答案 2 :(得分:1)

l = <your-list>
import re
sorted(l, key=lambda x:int(re.match(r'(\d+)',x).groups()[0]))

输出:

['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',
 '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']

答案 3 :(得分:0)

您可以通过这种方式按列表的第一个元素排序。

iterable.sort(key=lambda x: x[0])
print iterable