Register-case sorting without decreasing the result?

I have a list of lines like this:

['Aden', 'abel'] 

I want to sort case-insensitive items. Therefore, I want to get:

 ['abel', 'Aden'] 

But I get the opposite value with sorted() or list.sort() , because lowercase is displayed in upper case.

How can I ignore the matter? I have seen solutions that include reducing all list items, but I don't want to change the case of the list items.

+108
python string sorting list case-insensitive
Apr 22 '12 at 16:20
source share
7 answers

Python 3. 3+ has a str.casefold method specifically designed for case insensitive matching:

 sorted_list = sorted(unsorted_list, key=str.casefold) 

In Python 2, use lower() :

 sorted_list = sorted(unsorted_list, key=lambda s: s.lower()) 

It works for both regular and unicode strings, as they both have a lower method.

In Python 2, this works for a mixture of regular and unicode strings, as the values โ€‹โ€‹of the two types can be compared with each other. However, Python 3 does not work this way: you cannot compare a byte string and a string in Unicode, so in Python 3 you have to perform reasonable actions and sort lists of only one type of string.

 >>> lst = ['Aden', u'abe1'] >>> sorted(lst) ['Aden', u'abe1'] >>> sorted(lst, key=lambda s: s.lower()) [u'abe1', 'Aden'] 
+154
Apr 22 2018-12-22T00:
source share
 >>> x = ['Aden', 'abel'] >>> sorted(x, key=str.lower) # Or unicode.lower if all items are unicode ['abel', 'Aden'] 

In Python 3, str is unicode, but in Python 2 you can use this more general approach, which works for both str and unicode :

 >>> sorted(x, key=lambda s: s.lower()) ['abel', 'Aden'] 
+42
Apr 22 '12 at 16:21
source share

You can also try the following:

 >>> x = ['Aden', 'abel'] >>> x.sort(key=lambda y: y.lower()) >>> x ['abel', 'Aden'] 
+9
Apr 22 '12 at 16:40
source share

In python3 you can use

 list1.sort(key=lambda x: x.lower()) #Case In-sensitive list1.sort() #Case Sensitive 
+3
Jul 30 '15 at 14:17
source share

This works in Python 3 and does not include lowercase result (!).

 values.sort(key=str.lower) 
+2
Aug 6 '19 at 22:13
source share

I did this for Python 3.3:

  def sortCaseIns(lst): lst2 = [[x for x in range(0, 2)] for y in range(0, len(lst))] for i in range(0, len(lst)): lst2[i][0] = lst[i].lower() lst2[i][1] = lst[i] lst2.sort() for i in range(0, len(lst)): lst[i] = lst2[i][1] 

Then you can simply call this function:

 sortCaseIns(yourListToSort) 
+1
Mar 18 '15 at 12:29
source share

try it

 def cSort(inlist, minisort=True): sortlist = [] newlist = [] sortdict = {} for entry in inlist: try: lentry = entry.lower() except AttributeError: sortlist.append(lentry) else: try: sortdict[lentry].append(entry) except KeyError: sortdict[lentry] = [entry] sortlist.append(lentry) sortlist.sort() for entry in sortlist: try: thislist = sortdict[entry] if minisort: thislist.sort() newlist = newlist + thislist except KeyError: newlist.append(entry) return newlist 



 lst = ['Aden', 'abel'] print cSort(lst) 

Exit

['abel', 'Aden']

-2
Jan 23 '13 at 5:56
source share



All Articles