Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list of strings ignoring upper/lower case

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 strings first and then lowercase.

But I need the below output.

Input:

var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant'] 

Output:

['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion'] 
like image 424
Darknight Avatar asked Dec 19 '12 14:12

Darknight


People also ask

Which method is appropriate for case-insensitive sorting for a list?

Case-insensitive Sorting By default, the sort() method sorts the list in ASCIIbetical order rather than actual alphabetical order. This means uppercase letters come before lowercase letters.

Which option represents case-insensitive sorting?

By “alphabetically,” those folks mean case-insensitive sort order, where uppercase/lowercase pairs (in the local language) are treated as the same letter.

How do you sort a list containing strings?

In Python, there are two ways, sort() and sorted() , to sort lists ( list ) in ascending or descending order. If you want to sort strings ( str ) or tuples ( tuple ), use sorted() .


1 Answers

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.

like image 61
Martijn Pieters Avatar answered Sep 24 '22 21:09

Martijn Pieters