Say I have these lists:
a = [1, 2, 3, 4]
b = [6,7]
c = ['a', 'b', 'c']
I would like to create a 3-Dimensional data structure that contains new lists combining all the elements of each list together in all possible ways. I would then like to access these new list using matrix like indexing.
So for example, say f is a function that does what I want. Then I could do this:
m = f(a,b,c)
Then m[0][0][0] would give [1,6,'a'], m[1][0][0] would give [2,6,'a'], and so on.
Basically, I know I can already do this using nested for loops.
jList = []
for j in a:
kList = []
for k in b:
lList = []
for l in c:
o = [j,k,l]
lList.append(o)
kList.append(lList)
jList.append(kList)
This gives me a list of lists of lists of lists.
[[[[1, 6, 'a'], [1, 6, 'b'], [1, 6, 'c']],
[[1, 7, 'a'], [1, 7, 'b'], [1, 7, 'c']]],
[[[2, 6, 'a'], [2, 6, 'b'], [2, 6, 'c']],
[[2, 7, 'a'], [2, 7, 'b'], [2, 7, 'c']]],
[[[3, 6, 'a'], [3, 6, 'b'], [3, 6, 'c']],
[[3, 7, 'a'], [3, 7, 'b'], [3, 7, 'c']]],
[[[4, 6, 'a'], [4, 6, 'b'], [4, 6, 'c']],
[[4, 7, 'a'], [4, 7, 'b'], [4, 7, 'c']]]]
This is fine, but I have to do this for dimensions higher than 3, and the many nested for loops just seems like it can't possibly be the best or most efficient way to build this type of data structure. I can't help but thinking there must be a better solution, something in some library like the function I made up above, maybe numpy function, but I've done a lot of searching and have not found something for lists of varying sizes and data types like I have.
Any ideas?