问题:如何对字符串列表进行排序?
在Python中创建按字母顺序排序的列表的最佳方法是什么?
What is the best way of creating an alphabetically sorted list in Python?
回答 0
基本答案:
mylist = ["b", "C", "A"]
mylist.sort()
这会修改您的原始列表(即就地排序)。要获得列表的排序副本,而无需更改原始副本,请使用以下sorted()
函数:
for x in sorted(mylist):
print x
但是,上面的示例有些天真,因为它们没有考虑区域设置,而是执行区分大小写的排序。您可以利用可选参数key
指定自定义排序顺序(使用的替代方法cmp
是不推荐使用的解决方案,因为它必须多次评估- key
每个元素仅计算一次)。
因此,要根据当前语言环境进行排序,并考虑到特定于语言的规则(这cmp_to_key
是functools的帮助函数):
sorted(mylist, key=cmp_to_key(locale.strcoll))
最后,如果需要,您可以指定自定义语言环境进行排序:
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']
最后要注意的是:您将看到使用该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)
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).
So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key
is a helper function from functools):
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:
# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)
回答 1
还值得注意的sorted()
功能:
for x in sorted(list):
print x
这将返回列表的新排序版本,而不更改原始列表。
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.
回答 2
list.sort()
It really is that simple :)
回答 3
字符串排序的正确方法是:
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']
前面的示例mylist.sort(key=lambda x: x.lower())
对于仅ASCII上下文适用。
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.
回答 4
请在Python3中使用sorted()函数
items = ["love", "like", "play", "cool", "my"]
sorted(items2)
Please use sorted() function in Python3
items = ["love", "like", "play", "cool", "my"]
sorted(items2)
回答 5
但是,这如何处理特定于语言的排序规则?是否考虑到语言环境?
不,list.sort()
是通用排序功能。如果要根据Unicode规则进行排序,则必须定义一个自定义的排序键函数。您可以尝试使用pyuca模块,但我不知道它的完整性。
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.
回答 6
这是一个老问题,但是如果您想在不进行设置的情况下进行 locale.LC_ALL
可感知区域设置的排序,则可以按照此答案的建议使用PyICU库:
import icu # PyICU
def sorted_strings(strings, locale=None):
if locale is None:
return sorted(strings)
collator = icu.Collator.createInstance(icu.Locale(locale))
return sorted(strings, key=collator.getSortKey)
然后用例如:
new_list = sorted_strings(list_of_strings, "de_DE.utf8")
这对我有用,而无需安装任何语言环境或更改其他系统设置。
(这已经在上面的评论中建议过,但是我想让它更加突出,因为我一开始就很想念它。)
Old question, but if you want to do locale-aware sorting without setting locale.LC_ALL
you can do so by using the PyICU library as suggested by this answer:
import icu # PyICU
def sorted_strings(strings, locale=None):
if locale is None:
return sorted(strings)
collator = icu.Collator.createInstance(icu.Locale(locale))
return sorted(strings, key=collator.getSortKey)
Then call with e.g.:
new_list = sorted_strings(list_of_strings, "de_DE.utf8")
This worked for me without installing any locales or changing other system settings.
(This was already suggested in a comment above, but I wanted to give it more prominence, because I missed it myself at first.)
回答 7
假设 s = "ZWzaAd"
要在字符串上方排序,简单的解决方案将是在字符串下方。
print ''.join(sorted(s))
Suppose s = "ZWzaAd"
To sort above string the simple solution will be below one.
print ''.join(sorted(s))
回答 8
或许:
names = ['Jasmine', 'Alberto', 'Ross', 'dig-dog']
print ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))
Or maybe:
names = ['Jasmine', 'Alberto', 'Ross', 'dig-dog']
print ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))
回答 9
l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
l.sort()
print(l1)
结果
[‘abc’,’ba’,’cd’,’dc’,’xy’]
l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
l.sort()
print(l1)
Result
[‘abc’, ‘ba’, ‘cd’, ‘dc’, ‘xy’]
回答 10
很简单:https :
//trinket.io/library/trinkets/5db81676e4
scores = '54 - Alice,35 - Bob,27 - Carol,27 - Chuck,05 - Craig,30 - Dan,27 - Erin,77 - Eve,14 - Fay,20 - Frank,48 - Grace,61 - Heidi,03 - Judy,28 - Mallory,05 - Olivia,44 - Oscar,34 - Peggy,30 - Sybil,82 - Trent,75 - Trudy,92 - Victor,37 - Walter'
得分= scores.split(’,’)for x in sorted(scores):print(x)
It is simple:
https://trinket.io/library/trinkets/5db81676e4
scores = '54 - Alice,35 - Bob,27 - Carol,27 - Chuck,05 - Craig,30 - Dan,27 - Erin,77 - Eve,14 - Fay,20 - Frank,48 - Grace,61 - Heidi,03 - Judy,28 - Mallory,05 - Olivia,44 - Oscar,34 - Peggy,30 - Sybil,82 - Trent,75 - Trudy,92 - Victor,37 - Walter'
scores = scores.split(‘,’)
for x in sorted(scores):
print(x)