Wish to pad numbers from 1 - 99 like so:
001 002 ... 099
- I can print the list
x = [x 1 for x in range(99)]
print(x)
- or make a generator object
def gen99():
gen = 10
end = 99
while gen < end:
for x in range (end):
yield gen
gen = 1
#f = gen99()
#next(f)
- I can pad a single number
Padded = "{:0=3}"
print(Padded.format(1))
- I cannot combine these things
CodePudding user response:
Generally, you need to call the generator properly:
def gen(start, end):
for x in range(start, end):
yield x
Padded = "{:0=3}"
for x in gen(10, 100):
print(Padded.format(x))
But you can use a generator comprehension as well:
generator = (f"{x:0=3}" for x in range(10, 100))
for el in generator:
print(el)
Edit:
If you want to use next
syntax:
generator = gen(10, 100)
element = next(generator)
print(Padded.format(element))
CodePudding user response:
Unless I don't understand the question then surely it's as simple as:
print(' '.join(f'{n:03}' for n in range(1, 100)))
Output:
001 002 002 ... 099
Note:
Not a list comprehension but a generator. Range is 1->99 inclusive