C
C
CrewCut2016-06-19 15:36:48
PHP
CrewCut, 2016-06-19 15:36:48

How to get all possible string "join" options from 4 or more arrays?

There are 4 arrays containing strings. You need to get a single array, which will contain all possible connections of strings from these arrays, despite the fact that the number of strings may not be the same in each of them. Using the example of arrays with one row:

Array 1 = ( 'a' );
Array 2 = ( 'б' );
Array 3 = ( 'в' );
Array 4 = ( 'г' );

result = ( 
'aб',
'ав',
'аг',
'бв',
'бг',
'вг',
'абвг'
 )

I just can not understand how to do this, what logic should be. As a simplification option, you can take all values, not only unique ones (for example:
аб, ав, аг, ба, бв, бг, ва, вб, вг, га, гб, гв, абвг
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2016-06-19
@CrewCut

number of permutations - n factorial
i.e. count = len(a)*len(b)*len(c)*..*len(x)
algorithm - banal for over nested arrays

a = ['a1','a2','a3',]
b = ['b1',]
c = ['c1','c2',]
d = ['d1','d2','d3','d4',]


total = []
for i in range(0, len(a)):
    for j in range(0, len(b)):
        for k in range(0, len(c)):
            for l in range(0, len(d)):
                total.append(a[i]+b[j]+c[k]+d[l])

print(total)
print(len(total))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question