1

If I have a array of an inconsistent size, lists that consists of an (inconsistent) amount of lists:

 lists = [List1, List2, List3,...ListN]

where each contained list is of inconsistent size.

How can I concatenate the contents multiplicatively of each contained array.

Example of target output:

A = ["A","B","C","D"]
B = ["1","2","3","4"]
C = ["D","E","F","G"]
A[0-N] + B[0-N] + C[0-N]

Giving ["A1D","B1D","C1D","D1D",
        "A2D","B2D","C2D","D2D"
        "A3D","B3D","C3D","D3D"
        "A4D","B4D","C4D","D4D"
        "A1E","B1E","C1E","D1E"

         ... "C4G","D4G"  ]

For this specific example it should yield a list length of 4^3. (List length to the power of the number of lists)

However list length is not constant so it is really

List1 Length * List2 Length * List3 Length * ... * ListN Length

For an inconsistent list length:

  A = ["A","B"]
  B = ["1","2","3","4"]

 = ["A1","A2","A3","A4","B1","B2","B3","B4"]

I have tried python maps and zips, but I am having trouble doing for example:

zip(list1, list2, list3)

when:

Amount of lists is not consistent

The lists are not stored separately but collated in one large list,

and the list size is not consistent

Methods described on other SO Question only address consistent size, 2 list, situations. I am having trouble applying these techniques in this situation.

1
  • @enderland edited to reflect. Commented Sep 20, 2016 at 15:06

3 Answers 3

1
import itertools
A = ["A","B"]
B = ["1","2","3","4"]
list(itertools.product(A, B))

Generic

lists_var = [List1, List2, List3,...ListN]
list(itertools.product(*lists_var))
Sign up to request clarification or add additional context in comments.

Comments

1

Use itertools to get the results, and then format as you want:

import itertools

A = ["A","B","C","D"]
B = ["1","2","3","4"]
C = ["D","E","F","G"]

lists = [A, B, C]

results = [''.join(t) for t initertools.product(*lists)]

print(results)

prints:

['A1D', 'A1E', 'A1F', 'A1G', 'A2D', 'A2E', 'A2F', 'A2G', 'A3D', 'A3E', 'A3F', 'A3G', 'A4D', 'A4E', 'A4F', 'A4G', 'B1D', 'B1E', 'B1F', 'B1G', 'B2D', 'B2E', 'B2F', 'B2G', 'B3D', 'B3E', 'B3F', 'B3G', 'B4D', 'B4E', 'B4F', 'B4G', 'C1D', 'C1E', 'C1F', 'C1G', 'C2D', 'C2E', 'C2F', 'C2G', 'C3D', 'C3E', 'C3F', 'C3G', 'C4D', 'C4E', 'C4F', 'C4G', 'D1D', 'D1E', 'D1F', 'D1G', 'D2D', 'D2E', 'D2F', 'D2G', 'D3D', 'D3E', 'D3F', 'D3G', 'D4D', 'D4E', 'D4F', 'D4G']

1 Comment

list() is unneccesary.
0

itertools.product Is what you are looking for, I think:

>>> import itertools
>>> A = ["A","B"]
>>> B = ["1","2","3","4"]
>>> C = [A,B]
>>> [''.join(i) for i in itertools.product(*C)]
['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4']

2 Comments

This doesn't handle a list of variable input size.
@enderland Good point - d'oh. Fixed. Thanks for pointing that out.

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.