Using python to sort string lists with case-insensitive characters

Source: Internet
Author: User
This article mainly introduces how to sort string lists by case-insensitive python, and how to sort strings by three different methods, for more information about how to sort string lists by case-insensitive python, see the following example. Share it with you for your reference. The specific analysis is as follows:

Let's take a look at the following code:

String = ''' the stirngHas manyline InTHE fIlejb51 net''' list _ of_string = string. split () print list_of_string # split the string and put it into the print '* 50def case_insensitive_sort (liststring): listtemp = [(x. lower (), x) for x in liststring] # convert the string list to generate tuples (case-insensitive strings, strings) listtemp. sort () # sort tuples because the tuples are: (case-insensitive strings, strings ), return [x [1] for x in listtemp] # after sorting is completed, return the print case_insensitive_sort (list_of_string) of the original string # Call and Test

Result:

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

Another method:

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

The official description of this function is as follows:

Return a new sorted list from the items in iterable.
Key specifies a function of one argument that is used to extract a comparison key from each list element: key = str. lower. The default value isNone.

Use the parameter key = str. lower

The complete code is as follows:

String = ''' the stirngHas manyline InTHE fIlejb51 net''' list _ of_string = string. split () print list_of_string # split the string and put it into the print '* 50def case_insensitive_sort2 (liststring): return sorted (liststring, key = str. lower) print case_insensitive_sort2 (list_of_string) # Call and Test

Same effect ~

Method 3:

Use the sort method of list:

The official description of this method is as follows:

The sort () method takes optional arguments for controlling the comparisons.
Cmp specifies a custom comparison function of two arguments (list items) which showould return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal, or larger than the second argument: cmp = lambda x, y: cmp (x. lower (), y. lower ()). the default value is None.
Key specifies a function of one argument that is used to extract a comparison key from each list element: key = str. 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 Code is as follows:

String = ''' the stirngHas manyline InTHE fIlejb51 net''' list _ of_string = string. split () print list_of_string # split the string and put it into the print '* 50def 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 call is different.

If you are interested, you can debug and run the examples in this article to deepen your impression. I believe there will be new gains!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.