noumenal
noumenal

Reputation: 1237

Create a List of Sentences based on Multiple Arrays of Integers and Words

I am trying to generate a list of strings from all possible combinations of multiple arrays.

l = ''

zero = [2, 3, 4, 5, 6]
one = ['boys', 'girls', 'pythons', 'baddies']
three = ['the', 'grandmas', 'bunny's']

# Do something with i, j, k. Iterate...
s = '{0!s} apples brings all the {1} to {2} yard'.format(zero[i], one[j], two[k])
l = l + s + '\n'

Expected output, l:

2 apples brings all the boys to the yard
3 apples brings all the boys to the yard
...
...
5 apples brings all the baddies to bunny's yard
6 apples brings all the baddies to bunny's yard

How can I generate a list that exhausts all possible i, j, k, combinations? The above is just an example. In the real world I have seven variables ([i] - [o]).

Upvotes: 0

Views: 22

Answers (1)

jimidime
jimidime

Reputation: 552

try itertools for finding combinations,

import itertools

l = ''

zero = [2, 3, 4, 5, 6]
one = ['boys', 'girls', 'pythons', 'baddies']
three = ['the', 'grandmas', 'bunny\'s']
s = '{0!s} apples brings all the {1} to {2} yard'

for combination in itertools.product(zero, one, three):
    l = l + s.format(combination[0], combination[1], combination[2]) + '\n'
print(l)

output:

2 apples brings all the boys to the yard
2 apples brings all the boys to grandmas yard
2 apples brings all the boys to bunnys yard
2 apples brings all the girls to the yard
2 apples brings all the girls to grandmas yard
2 apples brings all the girls to bunnys yard
2 apples brings all the pythons to the yard
2 apples brings all the pythons to grandmas yard
2 apples brings all the pythons to bunnys yard
2 apples brings all the baddies to the yard
2 apples brings all the baddies to grandmas yard
2 apples brings all the baddies to bunnys yard
3 apples brings all the boys to the yard
3 apples brings all the boys to grandmas yard
3 apples brings all the boys to bunnys yard
3 apples brings all the girls to the yard
3 apples brings all the girls to grandmas yard
3 apples brings all the girls to bunnys yard
3 apples brings all the pythons to the yard
3 apples brings all the pythons to grandmas yard
3 apples brings all the pythons to bunnys yard
3 apples brings all the baddies to the yard
3 apples brings all the baddies to grandmas yard
3 apples brings all the baddies to bunnys yard
4 apples brings all the boys to the yard
4 apples brings all the boys to grandmas yard
4 apples brings all the boys to bunnys yard
4 apples brings all the girls to the yard
4 apples brings all the girls to grandmas yard
4 apples brings all the girls to bunnys yard
4 apples brings all the pythons to the yard
4 apples brings all the pythons to grandmas yard
4 apples brings all the pythons to bunnys yard
4 apples brings all the baddies to the yard
4 apples brings all the baddies to grandmas yard
4 apples brings all the baddies to bunnys yard
5 apples brings all the boys to the yard
5 apples brings all the boys to grandmas yard
5 apples brings all the boys to bunnys yard
5 apples brings all the girls to the yard
5 apples brings all the girls to grandmas yard
5 apples brings all the girls to bunnys yard
5 apples brings all the pythons to the yard
5 apples brings all the pythons to grandmas yard
5 apples brings all the pythons to bunnys yard
5 apples brings all the baddies to the yard
5 apples brings all the baddies to grandmas yard
5 apples brings all the baddies to bunnys yard
6 apples brings all the boys to the yard
6 apples brings all the boys to grandmas yard
6 apples brings all the boys to bunnys yard
6 apples brings all the girls to the yard
6 apples brings all the girls to grandmas yard
6 apples brings all the girls to bunnys yard
6 apples brings all the pythons to the yard
6 apples brings all the pythons to grandmas yard
6 apples brings all the pythons to bunnys yard
6 apples brings all the baddies to the yard
6 apples brings all the baddies to grandmas yard
6 apples brings all the baddies to bunnys yard

Upvotes: 1

Related Questions