Possible Duplicate:
How do I sort a list of strings in Python?
How do I sort unicode strings alphabetically in Python?
我有一个字符串列表list
,希望按字母顺序排序。当我调用list.sort()
时,列表的第一部分包含以大写字母按字母顺序排序的条目,第二部分包含以小写字母开头的排序条目。就像这样:
Airplane
Boat
Car
Dog
apple
bicycle
cow
doctor
我在谷歌上寻找答案,但没有找到有效的算法。我读到了locale
模块和sort
参数cmp
和key
。通常情况下,在与sort
的一行中会有这样的lambda
,这让我无法更好地理解事情。
我怎么能从:
list = ['Dog', 'bicycle', 'cow', 'doctor', 'Car', 'Boat', 'apple', 'Airplane']
致:
Airplane
apple
bicycle
Boat
Car
cow
doctor
Dog
应考虑到外语的特点(如ä,î,î)。
不区分大小写的比较:
这实际上是python wiki about sorting上建议的方法:
这里似乎对这个主题有一个很好的概述: http://wiki.python.org/moin/HowTo/Sorting/
向下滚动一页到这里
例如,下面是不区分大小写的字符串比较:
相关问题 更多 >
编程相关推荐