Why does Python sort put upper case items first?

后端 未结 3 2159
暗喜
暗喜 2020-12-11 05:37

Not looking for a work around. Looking to understand why Python sorts this way.

>>> a = [\'aaa\',\'Bbb\']
>>> a.sort()
>>> print(a         


        
3条回答
  •  生来不讨喜
    2020-12-11 06:15

    Python treats uppercase letters as lower than lowercase letters. If you want to sort ignoring the case sensitivity. You can do something like this:

    a = ['aaa','Bbb']
    a.sort(key=str.lower)
    print(a)
    
    Outputs:
    ['aaa', 'Bbb']
    

    Which ignores the case sensitivity. The key parameter "str.lower" is what allows you to do this. The following documentation should help. https://docs.python.org/3/howto/sorting.html

提交回复
热议问题