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

前端 未结 6 1195
时光取名叫无心
时光取名叫无心 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:23

    Print the set of xl cell range of lowercase and uppercase charterers

    Upper_case:

    from string import ascii_uppercase
    import itertools
    def iter_range_strings(start_colu):
        for size in itertools.count(1):
            for string  in itertools.product(ascii_uppercase, repeat=size):
                yield "".join(string)
    
    input_colume_range = ['A', 'B']
    input_row_range= [1,2]
    for row in iter_range_strings(input_colume_range[0]):
        for colum in range(int(input_row_range[0]), int(input_row_range[1]+1)):
            print(str(row)+ str(colum))
        if row ==  input_colume_range[1]:
            break
    
    

    Result:

    A1
    A2
    B1
    B2
    
    0 讨论(0)
  • 2020-11-30 09:28

    In two lines (plus an import):

    from string import ascii_uppercase as ABC
    
    count = 100
    ABC+=' '
    [(ABC[x[0]] + ABC[x[1]]).strip() for i in range(count) if (x:= divmod(i-26, 26))]
    

    Wrap it in a function/lambda if you need to reuse.

    0 讨论(0)
  • 2020-11-30 09:43

    I've ended up doing my own. I think it can create any number of letters.

    def AA(n, s):
        r = n % 26
        r = r if r > 0 else 26
        n = (n - r) / 26
        s = chr(64 + r) + s
    
        if n > 26: 
            s = AA(n, s)
        elif n > 0:
            s = chr(64 + n) + s
    
        return s
    

    n = quantity | r = remaining (26 letters A-Z) | s = string

    To print the list :

    def uprint(nc):
        for x in range(1, nc + 1):
            print AA(x,'').lower()
    

    Used VBA before convert to python :

    Function AA(n, s)
    
        r = n Mod 26
        r = IIf(r > 0, r, 26)
        n = (n - r) / 26
        s = Chr(64 + r) & s
    
        If n > 26 Then
            s = AA(n, s)
        ElseIf n > 0 Then
            s = Chr(64 + n) & s
        End If
    
        AA = s
    
    End Function
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-30 09:47

    You can use a list comprehension.

    from string import ascii_lowercase
    L = list(ascii_lowercase) + [letter1+letter2 for letter1 in ascii_lowercase for letter2 in ascii_lowercase]
    
    0 讨论(0)
  • 2020-11-30 09:49

    Use itertools.product.

    from string import ascii_lowercase
    import itertools
    
    def iter_all_strings():
        for size in itertools.count(1):
            for s in itertools.product(ascii_lowercase, repeat=size):
                yield "".join(s)
    
    for s in iter_all_strings():
        print(s)
        if s == 'bb':
            break
    

    Result:

    a
    b
    c
    d
    e
    ...
    y
    z
    aa
    ab
    ac
    ...
    ay
    az
    ba
    bb
    

    This has the added benefit of going well beyond two-letter combinations. If you need a million strings, it will happily give you three and four and five letter strings.


    Bonus style tip: if you don't like having an explicit break inside the bottom loop, you can use islice to make the loop terminate on its own:

    for s in itertools.islice(iter_all_strings(), 54):
        print s
    
    0 讨论(0)
提交回复
热议问题