Python is built with a playful function-zip, and the ingenious implementation randomly generates a finite number of strings by probability.

Source: Internet
Author: User

Python has a more interesting built-in function-----zip, you can put the two sets of the incoming list to a combination of deformation, and then output the sub-element is a list of tuple, but the way of deformation is more abstract.

As an example:

a=[1,2,3,4,5,6]b=['a','B','c ','d']v1=zip (A, B)

The results of the V1 are:

' a ' ' b ' ' C ' ' D ')]

The more intuitive understanding is that A and B are two zippers on the button, the resulting list of sub-tuple, each group of tuple is a pair of buttons buttoned up, and from the first element of each group of list "pull zipper", that a zipper is from the 1~6,b zipper is from the a~d, pull to the end, Obviously pull to D on the button, so the result is obvious: 1-a,2-b,3-c,4-d. 5,6 staggered.

If this is to be given a matrix, such as 3x3, and output in the following way:

a1=[1,2,3]; a2=[4,5,6]; a3=[7,8,9]

A1,a2,a3 represent each row of the matrix, and now make the following changes:

a1=[1,2,3]a2=[4,5,6]a3=[7,8,9]zip (A1,A2,A3)

The output is:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Three zippers can also be pulled up ~

a=-------Zip--------b= Here you can see that it's a transpose of the original matrix.

This time let b1,b2,b3 distribution reverse output, that is, output B1[::-1],B2[::-2],B3[::-3], obviously, the result is a clockwise rotation of 90° results

Similarly, if a nested list is passed in, the result can be output by means of a *, for example:

Zip (*[(1, 4, 7), (2, 5, 8), (3, 6, 9)]) Zip (*[[1, 4, 7], [2, 5, 8], [3, 6, 9]])

The output results are:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

This very abstract function also has a magical magic----randomly generate a set of strings by probability

For example, for example, take 20 random strings, the candidate string includes a,b,c3, and the probability of each occurrence is called 0.1,0.3,0.6

def Random_pick (seq,probabilities):     = Random.uniform (0, 1)# First randomly generates a random number between 0 and 1    cumulative_probability = 0.0 for    in  zip (seq, probabilities): #seq代表待输入的字符串, prob represents the probability        of each corresponding string + = item_probability# Only jumps out when the cumulative probability is greater than the random number generated just now, and outputs the corresponding string         if x < cumulative_probability:              Break        return Item

For example:

First time x=0.09, the first cumulative probability is 0.1, satisfies the condition, jumps out and outputs the p=0.1 character

The second x=0.2, the first cumulative probability 0.1, does not meet and then accumulates to 0.4, jumps out the output p=0.3 the character

The third time x=0.3, the first cumulative probability 0.1, does not meet and then accumulates to 0.4, jumps out the output p=0.3 the character

The fourth time x=0.4, the first cumulative probability 0.1, does not meet and then accumulates to 0.4, still does not satisfy to accumulate to 1, jumps out the output p=0.6 character

Then x=0.5,0.6 .... 0.9, all output p=0.6 characters

It is obvious that such an output satisfies this probabilistic relationship and thus achieves the effect of outputting random strings by probability.

 for  in range:    print random_pick ("ABC", [0.1,0.3,0.6])

The output is:

Python is built with a playful function-zip, and the ingenious implementation randomly generates a finite number of strings by probability.

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.