Play Python each time one character is processed

Source: Internet
Author: User

The character in Python is a string of length 1, so you can iterate through a string, access each character in turn, and get the processing premise you want;

A list is a good idea, like this:thelist = List (thestring)

Of course, there is absolutely no list, and for those who like to iterate, they have a good reason to do so because there is no process to create a list:

For C in TheString:

Do_something_with (c)

Those who know the derivation of the list must not be bothered by the above notation, because the following code is something they are often proud of:

results = [Do_something_with (c) for C in TheString]

But no matter what you do, there will always be questions, but this is where Python is fun:

Results = map (Do_something_with, TheString)

If you want to get a collection of all the characters, because the collection has a special and wonderful operation:

1 Import sets 2 A = sets. Set ('My name is thinker')3 B = sets. Set ('Hello, thinker')4print'. Join (A & B)

Summary: Think of a simple problem, with different methods to solve, is a very enjoyable thing ....

 

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.