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

时间:2008-08-30 17:03:09

标签: python string sorting

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

11 个答案:

答案 0 :(得分:479)

基本答案:

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))

最后,如果需要,您可以指定custom locale进行排序:

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)

答案 1 :(得分:50)

值得注意的是sorted()功能:

for x in sorted(list):
    print x

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

答案 2 :(得分:35)

list.sort()

真的很简单:)

答案 3 :(得分:18)

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

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的上下文。

答案 4 :(得分:10)

  

但是,它如何处理特定于语言的排序规则?是否需要考虑区域设置?

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

答案 5 :(得分:9)

请在Python3中使用sorted()函数

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

答案 6 :(得分:0)

假设s = "ZWzaAd"

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

print ''.join(sorted(s))

答案 7 :(得分:0)

  

或者也许:

names = ['Jasmine', 'Alberto', 'Ross', 'dig-dog']
print ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))

答案 8 :(得分:0)

旧问题,但是如果您想无需设置就进行区域设置排序locale.LC_ALL可以按照PyICU library的建议使用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)

然后使用例如:

new_list = sorted_strings(list_of_strings, "de_DE.utf8")

这对我有用,而无需安装任何语言环境或更改其他系统设置。

(已经建议使用in a comment above,但是我想让它更加突出,因为我一开始就很想念它。)

答案 9 :(得分:0)

SELECT requestformtbl.employee_name, requestformtbl.request_type, requestformtbl.total_day,
                requestformtbl.request_status, requestformtbl.admin_remark, requestformtbl.confirmed_by, requestformtbl.date_confirmed, requesttbl.max_allotment,
                (requesttbl.max_allotment - sum(requestformtbl.total_day)) as Available from requestformtbl inner join requesttbl on 
                requestformtbl.request_type = requesttbl.request_type;

结果

  

['abc','ba','cd','dc','xy']

答案 10 :(得分:0)

很简单: 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(',') 对于x中的排序(分数): 打印(x)