Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

python - How to create an infinite iterator to generate an incrementing alphabet pattern?

I've created a function that generates a list of alphabets incrementing continuously. A, B, C ..., Z. After Z, it goes to AA, AB, AC ...AZ. This pattern repeats. This is similar to MS Excel's column names. At the moment, this function generates a finite list of alphabets.

_column_name_generator() = ['A', 'B', ..., 'AA', 'AB', ..., 'BA', 'BB', ..., 'CV']

I can then iterate over it in conjunction with some finite list, e.g. 0-10. See my code below. What I'd like is to create a generator that will give me an infinitely long list of incrementing alphabets.

import string


def _column_name_generator():
    column_names = []
    for x in range(0, 100):
        if x < 26:
            column_names.append(string.ascii_uppercase[x % 26])
        else:
            column_names.append(column_names[x/26 - 1] + string.ascii_uppercase[x % 26])
    return column_names

container = []
for column_name, num in zip(_column_name_generator(), range(0, 10)):
    container.append(column_name + str(num))

print _column_name_generator()
print container

container = ['A0', 'B1', 'C2', 'D3', 'E4', 'F5', 'G6', 'H7', 'I8', 'J9']
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yield column_names's last element every time, and use itertools.count instead of range to provide infinite increase:

import itertools

def _column_name_generator():
    column_names = []
    for x in itertools.count():
        if x < 26:
            column_names.append(string.ascii_uppercase[x % 26])
        else:
            column_names.append(column_names[x/26 - 1] + string.ascii_uppercase[x % 26])
        yield column_names[-1]

A better solution, altering the original code but dismissing the need in a memory consuming list of column_names, would be

import itertools, string

def _column_name_generator():
    for i in itertools.count(1):
        for p in itertools.product(string.ascii_uppercase, repeat=i):
            yield ''.join(p)

it basically iterates over the product of length i the uppercase ascii letters (every sequence possible) when i is gradually increasing, starting from 1 (A, B, C).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...