[英]How to sort a list of strings?
在 Python 中创建按字母顺序排序的列表的最佳方法是什么?
Basic answer:基本答案:
mylist = ["b", "C", "A"]
mylist.sort()
This modifies your original list (ie sorts in-place).这会修改您的原始列表(即就地排序)。 To get a sorted copy of the list, without changing the original, use the
sorted()
function:要获得列表的排序副本,而不更改原始列表,请使用
sorted()
函数:
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).您可以利用可选参数
key
来指定自定义排序顺序(使用cmp
的替代方案是一个已弃用的解决方案,因为它必须被多次评估 - 每个元素只计算一次key
)。
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
是来自 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.最后一点:您将看到使用
lower()
方法的不区分大小写排序的示例 - 这些示例是不正确的,因为它们仅适用于 ASCII 字符子集。 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)
list.sort()
真的就是这么简单:)
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.sort(key=lambda x: x.lower())
示例适用于mylist.sort(key=lambda x: x.lower())
ASCII 上下文。
Please use sorted() function in Python3请在 Python3 中使用 sorted() 函数
items = ["love", "like", "play", "cool", "my"]
sorted(items2)
But how does this handle language specific sorting rules?
但这如何处理特定于语言的排序规则? Does it take locale into account?
它是否考虑了语言环境?
No, list.sort()
is a generic sorting function.不,
list.sort()
是一个通用的排序函数。 If you want to sort according to the Unicode rules, you'll have to define a custom sort key function.如果要根据 Unicode 规则进行排序,则必须定义自定义排序键函数。 You can try using the pyuca module, but I don't know how complete it is.
你可以尝试使用pyuca模块,但我不知道它有多完整。
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 :老问题,但如果您想在不设置
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)
Then call with eg:然后调用例如:
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.) (这已经在上面的评论中提出了,但我想更加突出它,因为我自己一开始就错过了。)
Suppose s = "ZWzaAd"
假设
s = "ZWzaAd"
To sort above string the simple solution will be below one.要在字符串上方排序,简单的解决方案将低于一个。
print ''.join(sorted(s))
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()))
l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
l.sort()
print(l1)
Result结果
['abc', 'ba', 'cd', 'dc', 'xy']
['abc', 'ba', 'cd', 'dc', 'xy']
It is simple: https://trinket.io/library/trinkets/5db81676e4很简单: 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) score = score.split(',') for x in sorted(scores): print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.