-3
from itertools import product
for d in product(range(10), repeat=4):
   if 7 in d :
      print(d)

This is supposed to print all numbers that have number 7, but what if I want the number that contains exactly one 7?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

0

Here is one approach:

for each d count the number of 7s, and select the ones that contain exactly one. (thanks @MrXcoder for the assist in the comments)

from itertools import product 
for d in product(range(10), repeat=4):
    if d.count(7) == 1:
        print(d)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80