Reputation: 971
I have 10 variables x1 to x10 and size of number of possible values for each variable ranges from 1 to 256.
x1 = [1,10,20,23,12]
x2 = ['string0','string1','string3']
I need to generate all possible configurations
1, string0,..... <till x10 value>
1, string1
1, string3
......
12, string0
12, string1
12, string3
I am currently nested for to loop over all x1 to x10 like
for i in x1:
for j in x2:
......
print(i,j,k,.....)
The output doesnt print beyond three or four for loops. Is there any better way to print all possible configurations into a file?
Upvotes: 0
Views: 68
Reputation: 12015
Just a list comprehension, iterating over both the lists x1
and x2
is enough
>>> from pprint import pprint
>>> res = [(a,b) for a in x1 for b in x2]
>>> pprint(res)
[(1, 'string0'),
(1, 'string1'),
(1, 'string3'),
(10, 'string0'),
(10, 'string1'),
(10, 'string3'),
(20, 'string0'),
(20, 'string1'),
(20, 'string3'),
(23, 'string0'),
(23, 'string1'),
(23, 'string3'),
(12, 'string0'),
(12, 'string1'),
(12, 'string3')]
Upvotes: 0
Reputation: 851
Use product
method from itertools
module:
>>> import itertools as it
>>> x1 = [1,10,20,23,12]
>>> x2 = ['string0','string1','string3']
>>> list(it.product(x1, x2))
[(1, 'string0'),
(1, 'string1'),
(1, 'string3'),
(10, 'string0'),
(10, 'string1'),
(10, 'string3'),
(20, 'string0'),
(20, 'string1'),
(20, 'string3'),
(23, 'string0'),
(23, 'string1'),
(23, 'string3'),
(12, 'string0'),
(12, 'string1'),
(12, 'string3')]
Upvotes: 3