Sort list of strings ignoring upper/lower case

后端 未结 3 956
青春惊慌失措
青春惊慌失措 2020-12-05 02:38

I have a list which contains strings representing animal names. I need to sort the list. If I use sorted(list), it will give the list output with uppercase stri

3条回答
  •  暖寄归人
    2020-12-05 03:06

    The sort() method and the sorted() function take a key argument:

    var.sort(key=lambda v: v.upper())
    

    The function named in key is called for each value and the return value is used when sorting, without affecting the actual values:

    >>> var=['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
    >>> sorted(var, key=lambda v: v.upper())
    ['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']
    

    To sort Ant before ant, you'd have to include a little more info in the key, so that otherwise equal values are sorted in a given order:

    >>> sorted(var, key=lambda v: (v.upper(), v[0].islower()))
    ['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']
    

    The more complex key generates ('ANT', False) for Ant, and ('ANT', True) for ant; True is sorted after False and so uppercased words are sorted before their lowercase equivalent.

    See the Python sorting HOWTO for more information.

提交回复
热议问题