Python method of itertools.product

itertools.product: a plurality of similar requirements iterables Cartesian product.

It is used in the form of:

itertools.product(*iterables, repeat=1),

product (X, repeat = 3) is equivalent to the product (X, X, X).

1. Direct use: generate tuples, then a list synthesis

Import the itertools 
AA = The itertools.product ([ ' Tibet ' , ' falls ' , ' water ' ], [ ' moon ' , ' Star ' ]) 
BB = List (AA)    # order generated Cartesian product, the default is the REPEAT 1 
Print (bb)

2. Assuming provided: repeat = 3

= list random_list (The itertools.product ([ ' Tibet ' , ' falls ' , ' water ' ], [ ' moon ' , ' Star ' ], = REPEAT. 3 ))
 Print (random_list)   # This list length 216

Why is it 216?

Firstly, the repeat parameters is not provided, the default is 1, generating the list of a length of 6 - which can be represented by mathematical permutations and combinations, from the first parameter  [ "Tibet ',' falls', 'water']  retrieves a value, there are three possible; the second argument  [ 'moon', 'Star']  retrieves a value, there are two possibilities; so 2 * 3 = 6 kinds of results.

Then, when the set repeat = 3 when that is the repeat = 1 (default) results after repeated two times (i.e. a total of the last three sets of results as a first layer) further permutations and combinations , from the first the results (six kinds result) may take one of six kinds of elements, the same token, an element taken from the second and third repeat might have six kinds of results, so they have a combination of 6 * 6 * 6 = 216 species.

 Of course, also be synthesized first and then a tuple consisting of the list.

3. so it can understand the results below are 27 kinds of it

List = random_list (The itertools.product ([ ' Tibet ' , ' falls ' , ' water ' ], = REPEAT. 3 ))
 Print (random_list)

Because the result of the first layer 3 is possible; do this three times , on the formation of three sets as a result of (the first layer each have three possible results ), then the permutations is 3 * 3 * 3 = 27 kinds of results.

4. If the randomly taken from the list elements will not be repeated several words ( the original list of elements does not repeat itself ), available random.sample method.

Import Random 
random.seed ( . 1)    # Set random seed, the same result can be used to detect whether the random number obtained consistent 

n = 2 
AA = random.sample (random_list, n)    # random list of n elements 
Print (AA )

That is, from the above results, 27 kinds, two kinds of random removed, to give:

5. This method can be used to coordinate generation random, generating another random coordinates Method: https://www.cnblogs.com/qi-yuan-008/p/12564710.html

List = random_list (The itertools.product (Range (l, 4), Range (1,2 )))
 Print (random_list) 

n = 2 
AA = random.sample (random_list, n)    # random list of n elements 
Print ( aa)

 

reference:

https://segmentfault.com/q/1010000007715239?_ea=1447720

https://www.cnblogs.com/xxxxxxxxx/p/11544432.html

Guess you like

Origin www.cnblogs.com/qi-yuan-008/p/12577249.html