如何对字符串列表进行排序?

时间:2021-02-18 15:59:23

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

在Python中创建按字母顺序排序的列表的最佳方式是什么?

7 个解决方案

#1


421  

Basic answer:

基本的回答:

mylist = ["b", "C", "A"]
mylist.sort()

This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:

这将修改您的原始列表(例如,就地排序)。要获得列表的已排序副本,无需更改原始副本,请使用已排序()函数:

for x in sorted(mylist):
    print x

However, the examples above are a bit naive, because they don't take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter key to specify custom sorting order (the alternative, using cmp, is a deprecated solution, as it has to be evaluated multiple times - key is only computed once per element).

但是,上面的示例有点幼稚,因为它们不考虑本地语言环境,并且执行区分大小写的排序。您可以利用可选参数键来指定自定义排序顺序(使用cmp的替代方法是不赞成的解决方案,因为必须对其进行多次计算——每个元素只计算一次键)。

So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key is a helper function from functools):

因此,根据当前语言环境进行排序,考虑到特定于语言的规则(cmp_to_key是function tools的助手函数):

sorted(mylist, key=cmp_to_key(locale.strcoll))

And finally, if you need, you can specify a custom locale for sorting:

最后,如果需要,可以指定自定义的区域设置用于排序:

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'),
  key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad']

Last note: you will see examples of case-insensitive sorting which use the lower() method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:

最后注意:您将看到使用lower()方法的不区分大小写排序的例子——它们是不正确的,因为它们只适用于字符的ASCII子集。这两个数据对于任何非英语数据都是错误的:

# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)

#2


45  

It is also worth noting the sorted() function:

同样值得注意的是sort()函数:

for x in sorted(list):
    print x

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

这将返回一个列表的新排序版本,而不更改原始列表。

#3


34  

list.sort()

It really is that simple :)

其实很简单:

#4


18  

The proper way to sort strings is:

对字符串进行排序的正确方法是:

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'), cmp=locale.strcoll) == [u'aa', u'Ab', u'ad']

# Without using locale.strcoll you get:
assert sorted((u'Ab', u'ad', u'aa')) == [u'Ab', u'aa', u'ad']

The previous example of mylist.sort(key=lambda x: x.lower()) will work fine for ASCII-only contexts.

mylist的前一个例子。排序(key=lambda x: x.lower()))对于仅用于ascii的上下文中是适用的。

#5


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.

不,list.sort()是一个通用的排序函数。如果您想根据Unicode规则进行排序,您必须定义一个自定义排序键函数。您可以尝试使用pyuca模块,但我不知道它是如何完成的。

#6


5  

Please use sorted() function in Python3

请在Python3中使用sort()函数

items = ["love", "like", "play", "cool", "my"]
sorted(items2)

#7


1  

Suppose s = "ZWzaAd"

假设s =“ZWzaAd”

To sort above string the simple solution will be below one.

要对字符串进行排序,简单的解决方案将在1以下。

print ''.join(sorted(s))

#1


421  

Basic answer:

基本的回答:

mylist = ["b", "C", "A"]
mylist.sort()

This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:

这将修改您的原始列表(例如,就地排序)。要获得列表的已排序副本,无需更改原始副本,请使用已排序()函数:

for x in sorted(mylist):
    print x

However, the examples above are a bit naive, because they don't take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter key to specify custom sorting order (the alternative, using cmp, is a deprecated solution, as it has to be evaluated multiple times - key is only computed once per element).

但是,上面的示例有点幼稚,因为它们不考虑本地语言环境,并且执行区分大小写的排序。您可以利用可选参数键来指定自定义排序顺序(使用cmp的替代方法是不赞成的解决方案,因为必须对其进行多次计算——每个元素只计算一次键)。

So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key is a helper function from functools):

因此,根据当前语言环境进行排序,考虑到特定于语言的规则(cmp_to_key是function tools的助手函数):

sorted(mylist, key=cmp_to_key(locale.strcoll))

And finally, if you need, you can specify a custom locale for sorting:

最后,如果需要,可以指定自定义的区域设置用于排序:

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'),
  key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad']

Last note: you will see examples of case-insensitive sorting which use the lower() method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:

最后注意:您将看到使用lower()方法的不区分大小写排序的例子——它们是不正确的,因为它们只适用于字符的ASCII子集。这两个数据对于任何非英语数据都是错误的:

# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)

#2


45  

It is also worth noting the sorted() function:

同样值得注意的是sort()函数:

for x in sorted(list):
    print x

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

这将返回一个列表的新排序版本,而不更改原始列表。

#3


34  

list.sort()

It really is that simple :)

其实很简单:

#4


18  

The proper way to sort strings is:

对字符串进行排序的正确方法是:

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'), cmp=locale.strcoll) == [u'aa', u'Ab', u'ad']

# Without using locale.strcoll you get:
assert sorted((u'Ab', u'ad', u'aa')) == [u'Ab', u'aa', u'ad']

The previous example of mylist.sort(key=lambda x: x.lower()) will work fine for ASCII-only contexts.

mylist的前一个例子。排序(key=lambda x: x.lower()))对于仅用于ascii的上下文中是适用的。

#5


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.

不,list.sort()是一个通用的排序函数。如果您想根据Unicode规则进行排序,您必须定义一个自定义排序键函数。您可以尝试使用pyuca模块,但我不知道它是如何完成的。

#6


5  

Please use sorted() function in Python3

请在Python3中使用sort()函数

items = ["love", "like", "play", "cool", "my"]
sorted(items2)

#7


1  

Suppose s = "ZWzaAd"

假设s =“ZWzaAd”

To sort above string the simple solution will be below one.

要对字符串进行排序,简单的解决方案将在1以下。

print ''.join(sorted(s))