Python2.7 an example of permutation and combination of N arrays based on the Cartesian Product algorithm, python2.7
This example describes how Python2.7 performs the permutation and combination of N arrays based on the Cartesian Product algorithm. We will share this with you for your reference. The details are as follows:
Note: I encountered the problem of finding all the permutation and combination of n arrays some time ago. I found that the Cartesian Product algorithm can solve the problem. However, only Java version is available for online search, so I tried to implement it in python, because the novice code is not standardized.
Code: I encapsulate a class Cartesian (Cartesian), which encapsulates variables and methods:
1. Variables
Parallel roup: indicates a set of n lists (the list in python is similar to the array definition in other programming), that is, a two-dimensional array
CounterIndex: reverse downlink Value
Counter: used to record the subscript output by each array in the current rule roup. The initial value is 0, because the output starts from the first one.
2. Method
Countlength: calculates the length of an array, that is, the specific value of n.
Handle: process the lower mark value output by each one-dimensional array in the datagoroup two-dimensional array
Assemble: arranges and combines each element in n one-dimensional arrays in the parallel oroup.
#-*-Coding: UTF-8-*-# python implements the arrangement and combination of N arrays (Cartesian Product algorithm) class Cartesian (): # initialize def _ init _ (self, role roup): self. secondary roup = primary roup # Two-dimensional array from the back to the bottom mark value self. counterIndex = len (random roup)-1 # Each time the array value is output, the value array (initialized to 0) self. counter = [0 for I in range (0, len (self. calculate the length of the array def countlength (self): I = 0 length = 1 while (I <len (self. failed roup): length * = len (self. merge roup [I]) I + = 1 return length # recursively process the output subscript def handle (self): # locate the output subscript array and start from the last increment self. counter [self. counterIndex] + = 1 # determine whether the last digit of the positioning array has exceeded the length. The first vertex has traversed and ended if self. counter [self. counterIndex]> = len (self. role roup [self. counterIndex]): # reset the last subscript self. counter [self. counterIndex] = 0 # mark the first self in counter. counterIndex-= 1 # if self. counterIndex> = 0: self. handle () # reset the flag self. counterIndex = len (self. sort roup)-1 # arrange the output def assemble (self): length = self. countlength () I = 0 while (I <length): attrlist = [] j = 0 while (j <len (self. role roup): attrlist. append (self. role roup [j] [self. counter [j]) j + = 1 print attrlist self. handle () I + = 1
Test:
Note: In the test code, I only select a 3-dimensional array.
If _ name _ = "_ main _": # construct a two-dimensional array named roup = [['aa1',], ['bb1', 'bb2'], ['cc1', 'cc2', 'cc3'] # create the cartesian object cartesian = Cartesian (role roup) cartesian. assemble ()
Output result:
Note: This algorithm is implemented using python2.7.