在较新的Python中,我可以使用sorted函数,并根据它们的最后几个字符轻松地排序字符串列表:
lots_list=['anything']
print sorted(lots_list, key=returnlastchar)
def returnlastchar(s):
return s[10:]
Run Code Online (Sandbox Code Playgroud)
如何实现上述lots_list.sort()Python(2.3)中使用的上述内容?
"错误:当我尝试使用时sorted(),the global name sorted is not defined."
谢谢!
当我们得到的所有内容都是dict.sort()不带参数时,我们如何按Python 2.3中的值对字典进行排序.我可以使用sorted(dict.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)更新的Python 进行排序.
需要的是:示例:
d = {'a':2,'b':1,'c':3}
Run Code Online (Sandbox Code Playgroud)
排序后:
[('c':3),('a':2),('b':1)]
Run Code Online (Sandbox Code Playgroud)
有什么提示吗?谢谢 !