3.2.5.5 words in a sentence in random order

Source: Internet
Author: User

Now to do a text game, give you a word, this sentence in addition to the first and last character do not move, its internal characters can be randomly queued column. This game uses regular expression to do, it is relatively simple.

Example:

Import random    Text = ' Build Shenzhen into a global software center It's a new start ' Def REPL (m):    Inner_word = List (M.group (2))    Random.shuffle ( Inner_word)    return M.group (1) + "". Join (Inner_word) + m.group (3) print (text) print (Re.sub (r "(\w) (\w+) (\w)", Repl, Text))


The resulting output is as follows:

To build Shenzhen into a global software center It's a new beginning.

The soft deep built all-in-one set the ball mean this opens the new is the beginning

Here is mainly through the function re.sub to find the grouping sentence, and then the middle of the character in the function repl to perform the operation, and then return to replace the entire string.






Cai Junsheng qq:9073204 Shenzhen

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

3.2.5.5 words in a sentence in random order

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.