简体   繁体   English

在 Python 2.3 中按最后 N 个字符对字符串列表进行排序

[英]Sort a list of strings by last N characters in Python 2.3

In newer Python, I am able to use the sorted function and easily sort out a list of strings according to their last few chars as such:在较新的 Python 中,我能够使用sorted函数并根据字符串的最后几个字符轻松地整理出字符串列表,例如:

lots_list = ['anything']

print sorted(lots_list, key=returnlastchar)

def returnlastchar(s):     
    return s[10:] 

How can I implement the above to lots_list.sort() in older Python (2.3)?如何在较旧的 Python (2.3) 中对lots_list.sort()实现上述内容?

" Error: When I tried using sorted() , the global name sorted is not defined . " “错误:当我尝试使用sorted()the global name sorted is not defined 。”

The Schwartzian transform is usually more efficient than using the cmp argument (This is what newer versions of Python do when using the key argument) Schwartzian 变换通常比使用cmp参数更有效(这是使用key参数时较新版本的 Python 所做的)

lots_list=['anything']

def returnlastchar(s):     
    return s[10:] 

decorated = [(returnlastchar(s), s) for s in lots_list]
decorated.sort()
lots_list = [x[1] for x in decorated]

I don't have python 2.3 on hand, however, according to this post Sorting a list of lists by item frequency in Python 2.3 http://docs.python.org/release/2.3/lib/typesseq-mutable.html this method should also works for you.但是,我手头没有 python 2.3,根据这篇文章在 Python 2.3 中按项目频率排序列表列表http://docs.python.org/release/2.3/lib/typesseq-mutable.html这个方法也应该适合你。

def mycmp(a, b):
    return cmp(a[10:], b[10:])

lots_list.sort(mycmp)

It's not hard to write you're own version of sorted .编写自己的sorted版本并不难。 Here is a drop-in replacement (excluding the cmp paramenter):这是一个直接替换(不包括cmp参数):

def _count():
    i = 0
    while 1:
        yield i
        i += 1

def sorted(iterable, key=None, reverse=False):
    'Drop-in replacement for the sorted() built-in function (excluding cmp())'
    seq = list(iterable)
    if reverse:
        seq.reverse()
    if key is not None:
        seq = zip(map(key, seq), _count(), seq)
    seq.sort()
    if key is not None:
        seq = map(lambda decorated: decorated[2], seq)
    if reverse:
        seq.reverse()
    return seq

You can write your own sorted() like so:您可以像这样编写自己的sorted()

try:
    sorted
except NameError:
    def sorted(seq, key=None):
        lst = list(seq)  # get copy of list
        if key is not None:
            def my_cmp(a, b):
                return cmp(key(a), key(b))
        else:
            my_cmp = cmp
        lst.sort(my_cmp)
        return lst

This will only define your new sorted() if there is no built-in sorted() .如果没有内置sorted()这只会定义您的新sorted() First, we try to evaluate the name sorted and if we get a NameError we define our own.首先,我们尝试评估已sorted的名称,如果出现NameError我们定义自己的名称。 I'm using map(None, seq) as a fast way to make a new list out of the values of seq .我使用map(None, seq)作为从seq的值中创建新列表的快速方法。

Or, if we want to use the Schwartzian Transform for maximum efficiency as suggested by @gnibbler:或者,如果我们想按照@gnibbler 的建议使用 Schwartzian 变换来获得最大效率:

try:
    sorted
except NameError:
    import operator as op
    def sorted(seq, key=None):
        if key is not None:
            lst = [(key(x), x) for x in seq]
            lst.sort()
            return map(lambda x: x[1], lst)
        else:
            lst = list(seq) # get list from sequence
            lst.sort()
            return lst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM