0

I have an array which holds other arrays which are holding possible values of data at given position. Example:

data = [['A'],['A','B'],['A','B'],['A','B','D'],['0','2']]  

From this data possible values are (for example):

"AAAA0"  # (00000)  
"AAAA2"  # (00001)  
"AAAD0"  # (00020)  

and so on.
What I would need, is to get all possible combinations of data from those single arrays, but order of data is important:

  • data from inner array can only be placed on its position in outer array (in above example on first position only 'A' can be placed)

Is there some Python module that will be able to handle this (I found itertools, but it's doing not exactly what I need), or maybe someone has some idea how to do this ?

3
  • 1
    This is not a code-writing service. Could you show us some code that you have tried so we can say what the problems are? You need to show more effort of your own. Commented Jul 31, 2016 at 22:45
  • I've tried this: import itertools print (list(itertools.combinations([['A'],['B','C'],['D','E','F']], 2))) with result: [(['A'], ['B', 'C']), (['A'], ['D', 'E', 'F']), (['B', 'C'], ['D', 'E', 'F'])] Commented Jul 31, 2016 at 22:46
  • Possible duplicate of Get the cartesian product of a series of lists in Python Commented Jul 31, 2016 at 22:58

2 Answers 2

1

I think you need the itertools.product here:

import itertools
[''.join(p) for p in itertools.product(*data)]

#['AAAA0',
# 'AAAA2',
# 'AAAB0',
# 'AAAB2',
# 'AAAD0',
# 'AAAD2',
# 'AABA0',
# 'AABA2',
# 'AABB0',
# 'AABB2',
# 'AABD0',
# 'AABD2',
# 'ABAA0',
# 'ABAA2',
# 'ABAB0',
# 'ABAB2',
# 'ABAD0',
# 'ABAD2',
# 'ABBA0',
# 'ABBA2',
# 'ABBB0',
# 'ABBB2',
# 'ABBD0',
# 'ABBD2']
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to do exactly what I needed. Thank for fast help to all who responded. Also answer of David Gomes was helpful, but this one-liner does the job.
0

Try this:

data = [['A'], ['A','B'], ['A','B'], ['A','B','D'], ['0','2']]
size = 5

def rec(cur):
    if len(cur) == size:
        print(cur)
        return

    for x in data[len(cur)]:
        rec(cur + [x])

rec([])

Output:

['A', 'A', 'A', 'A', '0']
['A', 'A', 'A', 'A', '2']
['A', 'A', 'A', 'B', '0']
['A', 'A', 'A', 'B', '2']
['A', 'A', 'A', 'D', '0']
['A', 'A', 'A', 'D', '2']
['A', 'A', 'B', 'A', '0']
['A', 'A', 'B', 'A', '2']
['A', 'A', 'B', 'B', '0']
['A', 'A', 'B', 'B', '2']
['A', 'A', 'B', 'D', '0']
['A', 'A', 'B', 'D', '2']
['A', 'B', 'A', 'A', '0']
['A', 'B', 'A', 'A', '2']
['A', 'B', 'A', 'B', '0']
['A', 'B', 'A', 'B', '2']
['A', 'B', 'A', 'D', '0']
['A', 'B', 'A', 'D', '2']
['A', 'B', 'B', 'A', '0']
['A', 'B', 'B', 'A', '2']
['A', 'B', 'B', 'B', '0']
['A', 'B', 'B', 'B', '2']
['A', 'B', 'B', 'D', '0']
['A', 'B', 'B', 'D', '2']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.