How to sort a list of strings?

后端 未结 11 788
孤独总比滥情好
孤独总比滥情好 2020-11-22 11:22

What is the best way of creating an alphabetically sorted list in Python?

相关标签:
11条回答
  • 2020-11-22 11:51

    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.

    0 讨论(0)
  • 2020-11-22 11:53

    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)
    
    0 讨论(0)
  • 2020-11-22 11:54

    Suppose s = "ZWzaAd"

    To sort above string the simple solution will be below one.

    print ''.join(sorted(s))
    
    0 讨论(0)
  • 2020-11-22 11:56

    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()))
    
    0 讨论(0)
  • 2020-11-22 11:57
    l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
    l.sort()
    print(l1)
    

    Result

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

    0 讨论(0)
提交回复
热议问题