a lazy version of this (using itertools.product
) would be:
from itertools import product
base = 3
n_digits = 5
last_digits = list(range(base)) # [0, 1, 2]
remaining_digits = product(range(base), repeat=n_digits - 1)
for digits in remaining_digits:
print(digits)
# (0, 0, 0, 0)
# (0, 0, 0, 1)
# (0, 0, 0, 2)
# ...
# (2, 2, 2, 0)
# (2, 2, 2, 1)
# (2, 2, 2, 2)
for any possible last_digits
you get remaining_digits
that you can prepend to them. the remaining_digits
here is an iterator - it will not store all the elements in memory.
you can then combine last_digits
and remaining_digits
any way you want (e.g. combine them to a str
ing or an int
eger as needed).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…