python中的排序列表

发布于 2024-10-28 09:12:51 字数 192 浏览 5 评论 0原文

如果我有一个字符串列表,例如 ["a143.txt", "a9.txt", ] 如何按列表中的数字而不是字符串按升序对其进行排序。即我希望 "a9.txt" 出现在 "a143.txt" 之前,因为 9 143..

谢谢。

if I have a list of strings e.g. ["a143.txt", "a9.txt", ] how can I sort it in ascending order by the numbers in the list, rather than by the string. I.e. I want "a9.txt" to appear before "a143.txt" since 9 < 143.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愁杀 2024-11-04 09:12:51

这就是所谓的“自然排序”,
来自 http://www.codinghorror.com/blog/2007 /12/sorting-for- humans-natural-sort-order.html

试试这个:

import re 

def sort_nicely( l ): 
  """ Sort the given list in the way that humans expect. 
  """ 
  convert = lambda text: int(text) if text.isdigit() else text 
  alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] 
  l.sort( key=alphanum_key ) 

It's called "natural sort order",
From http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

Try this:

import re 

def sort_nicely( l ): 
  """ Sort the given list in the way that humans expect. 
  """ 
  convert = lambda text: int(text) if text.isdigit() else text 
  alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] 
  l.sort( key=alphanum_key ) 
提笔书几行 2024-11-04 09:12:51

使用 list.sort() 并为 key 参数提供您自己的函数。将为列表中的每个项目调用您的函数(并传递该项目),并且预计返回将被排序的该项目的版本。

请参阅http://wiki.python.org/moin/HowTo/Sorting/#Key_Functions< /a> 了解更多信息。

Use list.sort() and provide your own function for the key argument. Your function will be called for each item in the list (and passed the item), and is expected to return a version of that item that will be sorted.

See http://wiki.python.org/moin/HowTo/Sorting/#Key_Functions for more information.

╰つ倒转 2024-11-04 09:12:51

如果你想完全忽略字符串,那么你应该这样做

import re
numre = re.compile('[0-9]+')
def extractNum(s):
    return int(numre.search(s).group())

myList = ["a143.txt", "a9.txt", ]
myList.sort(key=extractNum)

If you want to completely disregard the strings, then you should do

import re
numre = re.compile('[0-9]+')
def extractNum(s):
    return int(numre.search(s).group())

myList = ["a143.txt", "a9.txt", ]
myList.sort(key=extractNum)
似梦非梦 2024-11-04 09:12:51
>>> paths = ["a143.txt", "a9.txt"]
>>> sorted(paths, key=lambda s: int(re.search("\d+", s).group()))
['a9.txt', 'a143.txt']

更通用的是,如果您希望它也适用于以下文件:a100_32_12(并按数字组排序):

>>> paths = ["a143_2.txt", "a143_1.txt"]
>>> sorted(paths, key=lambda s: map(int, re.findall("\d+", s)))
['a143_1.txt', 'a143_1.txt']
>>> paths = ["a143.txt", "a9.txt"]
>>> sorted(paths, key=lambda s: int(re.search("\d+", s).group()))
['a9.txt', 'a143.txt']

More generic, if you want it to work also for files like: a100_32_12 (and sorting by numeric groups):

>>> paths = ["a143_2.txt", "a143_1.txt"]
>>> sorted(paths, key=lambda s: map(int, re.findall("\d+", s)))
['a143_1.txt', 'a143_1.txt']
帅气尐潴 2024-11-04 09:12:51

list.sort() 已弃用(请参阅 Python.org 操作方法)。 sorted(list, key=keyfunc) 更好。

import re

def sortFunc(item):
  return int(re.search(r'[a-zA-Z](\d+)', item).group(1))

myList = ["a143.txt", "a9.txt"]

print sorted(myList, key=sortFunc)

list.sort() is deprecated (see Python.org How-To) . sorted(list, key=keyfunc) is better.

import re

def sortFunc(item):
  return int(re.search(r'[a-zA-Z](\d+)', item).group(1))

myList = ["a143.txt", "a9.txt"]

print sorted(myList, key=sortFunc)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文