Python uses case-insensitive sorting to sort string lists. python uses case-insensitive sorting.

Source: Internet
Author: User

Python uses case-insensitive sorting to sort string lists. python uses case-insensitive sorting.

This article describes how to sort string lists by case-insensitive python. 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!


Python is sorted by the number of strings

Ideas:
Typical usage of the dictionary type. The dictionary type is used to count the number of occurrences, the string as the key, and the number of occurrences as the value.
The Code is as follows: you can modify it according to your needs. This removes leading and trailing spaces of the string and does not count empty strings.

PS: I changed it again, so the effect will be better.
# -- Coding: GB2312 --
Dic ={}# define a dictionary type
Fp = open('data.txt ') # open the file to be queried
For line in fp: # Read rows from fp. This method can be used to avoid reading by empty rows.
Line = line. strip () # Remove the leading Blank
If (''= line ):
Continue # Remove the leading and trailing blank fields. If it is empty rows, it will not be processed.
If (line in dic): # determines whether s is in the dictionary. if 1 is added to the statistics
Dic [line] + = 1
Else: # If not, add a new key for the first time in statistics. The initial value of the key is 1.
Dic [line] = 1
Fp. close () # Read the file and close the file.
# Sort by value. A list of tuples is returned.
AfterSort = sorted (dic. items (), key = lambda dic: dic [1])
Print afterSort # print the sorted list, which can be extracted and printed as needed

Result:
Data.txt contains
Qiang
Song
Wan
Qiang
Song
Qiang
Execute python and print it out:
[('Wan ', 1), ('song', 2), ('qiang', 3)]

Python string sorting

Print (''. join (sorted (a, key = lambda x: ord (x. lower () * 2 + x. islower ())))

Related Article

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.