"Python" Python Gets the most occurrences/fewer characters in a string

Source: Internet
Author: User

This section mainly achieves the following objectives:

1. Implement Python to get the most occurrences/fewer characters in a string

2. Analysis and treatment of existing problems

3. Optimization scheme

1. Implement Python to get the most occurrences/fewer characters in a string: 2. Problem analysis and Processing: Code Description:

The 1, max () method returns the maximum value for a given parameter, which can be a sequence. Refer to the Novice tutorial for details.

2. The Python count () method is used to count the number of occurrences of a character in a string. The optional parameters are the start and end positions of the string search. Refer to the Novice tutorial for details.

There is a problem with the code:

Issue1, the input string may be a full English character, or it may be a non-full English character. Examples include Chinese characters, numeric characters, special characters, and so on. In this case, Getmaxchar () will likely not return the expected value. Further optimization is required.

The value returned by Issue2, Getminchar, is often not the expected value. As in the example above, it is expected to return m, actually the value returned is B.

There is a problem root cause analysis:

The sequence of arguments given in the Issue1, Max method is the execution result of String.ascii_lowercase: ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '. When the count () method is used, only the number of characters that exist in the sequence can be counted.

3, Optimization scheme optimization scheme 1: First to go to the weight, and then go to the heavy after the sequence as Max, min parameters.

Case: Chinese string

Case: Full word character

Case: All special characters

Case: Mixed character

"Python" Python Gets the most occurrences/fewer characters in a string

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.