在较新的Python中,我可以使用sorted函数,并根据字符串的最后几个字符轻松地将其排序:
lots_list=['anything']
print sorted(lots_list, key=returnlastchar)
def returnlastchar(s):
return s[10:]
我如何实现上面的lots_list.sort()
,这是在旧版Python(2.3)中使用的?在
“错误:当我尝试使用sorted()
,the global name sorted is not defined
。”在
谢谢!在
Tags:
不过,根据这篇文章,我手头上没有Python2.3 Sorting a list of lists by item frequency in Python 2.3http://docs.python.org/release/2.3/lib/typesseq-mutable.html 这种方法也适用于您。在
编写自己的排序版本并不难。以下是一个替换项(不包括cmp参数):
Schwartzian transform通常比使用
cmp
参数更有效(这是较新版本的Python在使用key
参数时所做的工作)相关问题 更多 >
编程相关推荐