Python implements a way to sort a list of strings regardless of case

  • 2020-04-02 14:06:59
  • OfStack

This example shows how python can sort a list of strings regardless of case, which is a very useful technique. Share with you for your reference. Specific analysis is as follows:

Let's take a look at the following code:


string = '''
the stirng
Has many
line In
THE fIle
jb51 net
'''
list_of_string = string.split()
print list_of_string   # Separate the strings and put them in the list 
print '*'*50

def case_insensitive_sort(liststring):
  listtemp = [(x.lower(),x) for x in liststring]# List of strings, generate tuples, (ignore case string, string) 
  listtemp.sort()# Sort the tuples, because the tuples are :(ignoring case strings, strings), sort by case ignored strings 

  return [x[1] for x in listtemp]# When the sorting is complete, a list of the original strings is returned 

print case_insensitive_sort(list_of_string)# Call it and test it 

Results:


['the', 'stirng', 'Has', 'many', 'line', 'In', 'THE', 'fIle', 'jb51', 'net']
**************************************************
['fIle', 'Has', 'In', 'jb51', 'line', 'many', 'net', 'stirng', 'THE', 'the']

Another way:

Use built-in functions
Sorted (iterable [, CMP [, key [, reverse]]])

The official description of the function is as follows:

Return a new sorted list from the items in iterable.
Key specifies a function of one argument belong 2 extract a comparison key from each list element: key = STR. The lower. The default value isNone.

Use the parameter key= STR. Lower

The complete code is as follows:


string = '''
the stirng
Has many
line In
THE fIle
jb51 net
'''
list_of_string = string.split()
print list_of_string   # Separate the strings and put them in the list 
print '*'*50

def case_insensitive_sort2(liststring):
  return sorted(liststring,key = str.lower)

print case_insensitive_sort2(list_of_string)# Call it and test it 

Same effect ~

Method 3:

Use the sort method of list:

The official description of the method is as follows:

The sort () method takes optional arguments for controlling The comparisons.
CMP specifies a custom comparison function of two arguments (the list items) which should return a negative, and zero or positive number depending on been the first argument is considered smaller than, Equal to, or larger than the second argument: lambda x,y: CMP (x.lower(), y.lower()). The default value is None.
Key specifies a function of one argument belong 2 extract a comparison key from each list element: key = STR. The lower. The default value is None.
Reverse is a Boolean value. If set to True, then the list elements are sorted as If each comparison were reversed.

The specific code is as follows:


string = '''
the stirng
Has many
line In
THE fIle
jb51 net
'''
list_of_string = string.split()
print list_of_string   # Separate the strings and put them in the list 
print '*'*50

def case_insensitive_sort3(liststring):
  liststring.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))

case_insensitive_sort3(list_of_string)
print list_of_string

But this time it's different.

Interested friends can debug and run the example of this article to deepen the impression, I believe that there will be new harvest!


Related articles: