Python string sorting method and python string sorting method

Source: Internet
Author: User

Python string sorting method and python string sorting method

This article describes how to sort strings in Python in the form of examples. It is a very practical technique in Python programming. Share it with you for your reference. The specific method is as follows:

In general, sorting a string in python is quite troublesome:

1. The string type in python cannot be changed directly. You must first put the string to be sorted in the container, such as list.

2. The sort () function of the list container in python does not return a value.

Therefore, several lines of code are required to sort strings in python.

The specific implementation method is as follows:

>>> s = "string">>> l = list(s)>>> l.sort()>>> s = "".join(l)>>> s'ginrst'

Programmers who have just switched from C/C ++ and other languages will often feel very accustomed to it, because in C/C ++, these are all a line of statements that can be done. Therefore, a simple string sorting method is provided here.

The implementation code is as follows:

>>> s = "string">>> s = "".join((lambda x:(x.sort(),x)[1])(list(s)))>>> s'ginrst'

It is a little difficult to understand because lambda is used, but it is good to figure it out.

I hope this article will help you with Python programming.


Python string sorting

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

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)]

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.