How to sort a list of strings?

2019-01-01 06:35发布

What is the best way of creating an alphabetically sorted list in Python?

8条回答
谁念西风独自凉
2楼-- · 2019-01-01 07:10

But how does this handle language specific sorting rules? Does it take locale into account?

No, list.sort() is a generic sorting function. If you want to sort according to the Unicode rules, you'll have to define a custom sort key function. You can try using the pyuca module, but I don't know how complete it is.

查看更多
闭嘴吧你
3楼-- · 2019-01-01 07:17

It is also worth noting the sorted() function:

for x in sorted(list):
    print x

This returns a new, sorted version of a list without changing the original list.

查看更多
登录 后发表回答