Reputation: 53
I have three arrays
a = [2]
b = [2,3,6]
c = [1]
I want to merge them such that I get an array of size len(a)*len(b)
containing all permutations of both. (C will always contain a single value)
I thought that something like this would work
newArr = [for i in range len(a)*len(b) [for x in a][for y in b][for z in c]]
print(newArr)
[[2,2,1],[2,3,1],[2,6,1]]
However it doesn't seem to allow it within the syntax of the language. Does anyone have any clue as to how I do this with standard libraries?
Upvotes: 3
Views: 49
Reputation: 28948
import itertools
a = [2]
b = [2,3,6]
c = [1]
p = itertools.product(a, b, c)
print(list(p))
[(2, 2, 1), (2, 3, 1), (2, 6, 1)]
Upvotes: 1
Reputation: 44888
[[x, y, z] for x in a for y in b for z in c]
For example:
>>> [[x, y, z] for x in [2] for y in [2,3,6] for z in [1]]
[[2, 2, 1], [2, 3, 1], [2, 6, 1]]
Upvotes: 2