I'm trying to write a python script that would output:
000
001
002
... etc
but I'm running into difficulties. What I have so far is:
from itertools import product
list = [x for x in range(0, 10) if True]
for x in product(list, repeat=3):
list3 = list(x)
def convert(l):
c = [str(i) for i in l]
list2 = int("".join(c))
return(list2)
print(convert(list3))
but this only outputs:
999
I'm not sure how to get the full list. If I comment out the convert function it provides multiple lists of the numbers, like so:
[0, 0, 0]
[0, 0, 1]
...
Any help would be appreciated, I'm pretty sure I'm missing something simple.
You're overthinking it.
for n in range(0, 1000):
print(f'{n:03}')
import itertools
import string
for t in itertools.product(string.digits, repeat=3):
print("".join(t))
if you want to use itertools you can also do:
from itertools import combinations
lst = [x for x in range(0, 10) if True]
combos = list(combinations(lst, 3))
documentation on itertools.combinations is here: https://docs.python.org/3/library/itertools.html#itertools.combinations
You can try this
from itertools import permutations
digits = [0, 2, 3] #List of digits from which numbers are to be formed
n = 3 #number of digits in a number
num = [int(''.join(map(str, p))) for p in permutations(digits, n) if p[0] != 0]
print(num) #number formed are without repetition of digits
If repetition of digits are allowed, then you can try this
import itertools
rlist = list(itertools.permutations([1, 2, 3]))
plist = []
for r in rlist:
num = ''
for _ in r:
num += str(_)
plist.append(int(num))
print(plist)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.