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

本文探讨了在Python中如何正确对字符串列表进行排序,包括基本方法、考虑区域设置和自定义排序规则。建议使用内置的sort()或sorted()函数,并提供了针对不同情况的示例,如区分大小写、不区分大小写的排序以及特定于语言的排序规则。
摘要由CSDN通过智能技术生成

本文翻译自:How to sort a list of strings?

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


#1楼

参考:https://stackoom.com/question/9Ot/如何对字符串列表进行排序


#2楼

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())适用于仅限ASCII的上下文。


#3楼

list.sort()

它真的很简单:)


#4楼

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)

#5楼

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模块,但我不知道它有多完整。


#6楼

It is also worth noting the sorted() function: 值得注意的是sorted()函数:

for x in sorted(list):
    print x

This returns a new, sorted version of a list without changing the original list. 这将返回列表的新排序版本,而不更改原始列表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值