How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

前端 未结 6 1209
时光取名叫无心
时光取名叫无心 2020-11-30 08:53

I would like to make a alphabetical list for an application similar to an excel worksheet.

A user would input number of cells and I would like to generate list. For

6条回答
  •  难免孤独
    2020-11-30 09:46

    Following @Kevin 's answer :

    from string import ascii_lowercase
    import itertools
    
    # define the generator itself
    def iter_all_strings():
        size = 1
        while True:
            for s in itertools.product(ascii_lowercase, repeat=size):
                yield "".join(s)
            size +=1
    

    The code below enables one to generate strings, that can be used to generate unique labels for example.

    # define the generator handler
    gen = iter_all_strings()
    def label_gen():
        for s in gen:
            return s
    
    # call it whenever needed
    print label_gen()
    print label_gen()
    print label_gen()
    

提交回复
热议问题