代码之家  ›  专栏  ›  技术社区  ›  skolima

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

  •  358
  • skolima  · 技术社区  · 17 年前

    8 回复  |  直到 9 年前
        1
  •  552
  •   skolima    14 年前

    基本答案:

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

    sorted() 功能:

    for x in sorted(mylist):
        print x
    

    key cmp ,是不推荐使用的解决方案,因为它必须经过多次评估- 钥匙 每个元素只计算一次)。

    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)
    
        2
  •  57
  •   PrasadK    8 年前

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

    for x in sorted(list):
        print x
    

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

        3
  •  40
  •   rix0rrr    17 年前
    list.sort()
    

    其实很简单:)

        4
  •  18
  •   schmichael    16 年前

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

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

        5
  •  17
  •   Mahmud Ahsan    8 年前

    请在Python3中使用sorted()函数

    items = ["love", "like", "play", "cool", "my"]
    sorted(items2)
    
        6
  •  10
  •   John Millikin    17 年前

    list.sort() 是一个通用的排序函数。如果要根据Unicode规则进行排序,则必须定义自定义排序键函数。您可以尝试使用 pyuca

        7
  •  1
  •   vlz    6 年前

    老问题,但如果你想 无设置的区域设置感知排序 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 ,但我想让它更加突出,因为一开始我自己也错过了。)

        8
  •  0
  •   Sociopath    8 年前

    认为 s = "ZWzaAd"

    print ''.join(sorted(s))
    
        9
  •  0
  •   Dragos Alexe    7 年前

    或者可能:

    names = ['Jasmine', 'Alberto', 'Ross', 'dig-dog']
    print ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))
    
        10
  •  0
  •   asing177    6 年前
    l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
    l.sort()
    print(l1)
    

    后果

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

        11
  •  0
  •   Hedayatullah Sarwary    6 年前

    这很简单: 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'
    

    分数=分数。拆分(',') 打印(x)