Reputation: 73
I want to obtain a sequence like this
a,b,c,.....,x,y,z,aa,ab,ac,....,az,ba,bb,....,bz,....,aaa,....,azz....,zzz,...
and so on, until I achieve a given length.
My problem is that I don't know how to code an infinite number of loops.
abc = 'abcdefghijklmnopqrstuvwxyz'
def next_plate(i):
for letter in abc:
i += 1
yield letter, i
num_plates = 10000
i = 0
all_plates = {}
for plate,i in next_plate(i):
if i > num_plates:
break
all_plates[i] = plate
if i < num_plates:
for plate_1, _ in next_plate(i):
for plate_2,i in next_plate(i):
if i > num_plates:
break
all_plates[i] = plate_1 + plate_2
if i < num_plates:
for plate_1, _ in next_plate(i):
for plate_2, _ in next_plate(i):
for plate_3,i in next_plate(i):
if i > num_plates:
break
all_plates[i] = plate_1 + plate_2 + plate_3
Could someone help me with this?
Upvotes: 2
Views: 283
Reputation: 52008
Here is an itertools based generator:
import itertools, string
def plates():
n = 1
while True:
for plate in itertools.product(string.ascii_lowercase,repeat = n):
yield ''.join(plate)
n += 1
#test:
p = plates()
test = [next(p) for _ in range(10000)]
print(test[:30])
print(test[-30:])
output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad']
['nsm', 'nsn', 'nso', 'nsp', 'nsq', 'nsr', 'nss', 'nst', 'nsu', 'nsv', 'nsw', 'nsx', 'nsy', 'nsz', 'nta', 'ntb', 'ntc', 'ntd', 'nte', 'ntf', 'ntg', 'nth', 'nti', 'ntj', 'ntk', 'ntl', 'ntm', 'ntn', 'nto', 'ntp']
Upvotes: 2
Reputation: 11
You need to use the while loop and set it to true(adding the condition to break when your condition is met). I don't have my computer now so the answer is very brief.
Upvotes: 0