Reputation: 159
this is probably an easy problem, but my code is only executing the outer for loop at the end, and once at the beginning. It should loop over every combo for every combination of numbers
from itertools import permutations as p
combos = p(['/','*','-','+'], 3)
numbers = p(['9','7','7','6'])
for y in numbers:
print(y)
for x in combos:
print(x)
What am I doing wrong? It outputs:
('9', '7', '7', '6')
('/', '*', '-')
('/', '*', '+')
('/', '-', '*')
('/', '-', '+')
('/', '+', '*')
('/', '+', '-')
('*', '/', '-')
('*', '/', '+')
('*', '-', '/')
('*', '-', '+')
('*', '+', '/')
('*', '+', '-')
('-', '/', '*')
('-', '/', '+')
('-', '*', '/')
('-', '*', '+')
('-', '+', '/')
('-', '+', '*')
('+', '/', '*')
('+', '/', '-')
('+', '*', '/')
('+', '*', '-')
('+', '-', '/')
('+', '-', '*')
('9', '7', '6', '7')
('9', '7', '7', '6')
('9', '7', '6', '7')
('9', '6', '7', '7')
('9', '6', '7', '7')
('7', '9', '7', '6')
('7', '9', '6', '7')
('7', '7', '9', '6')
('7', '7', '6', '9')
('7', '6', '9', '7')
('7', '6', '7', '9')
('7', '9', '7', '6')
('7', '9', '6', '7')
('7', '7', '9', '6')
('7', '7', '6', '9')
('7', '6', '9', '7')
('7', '6', '7', '9')
('6', '9', '7', '7')
('6', '9', '7', '7')
('6', '7', '9', '7')
('6', '7', '7', '9')
('6', '7', '9', '7')
('6', '7', '7', '9')
Upvotes: 1
Views: 98
Reputation: 77059
itertools.permutations produces an iterator. That means it can be consumed. Once consumed, a subsequent loop will just skip it. If you convert it to a list, you will have continued access to its members.
from itertools import permutations as p
combos = list(p(['/','*','-','+'], 3))
numbers = list(p(['9','7','7','6']))
for y in numbers:
print(y)
for x in combos:
print(x)
Upvotes: 6