python中的连续字母列表并获取其所有值 [英] Continous alphabetic list in python and getting every value of it

查看:284
本文介绍了python中的连续字母列表并获取其所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了类似这样的问题:如何制作一个连续的字母列表python(从az到aa,ab,ac等)

I've almost the same problem like this one: How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

但是,我正在像excel一样在gui中做一个列表,其中垂直标题上应该是字母... aa,ab,ac .... dg,dh,di ...为此,我必须声明列表中的每个位置为特定字母.屈服可能是不可能的.

But, I am doing a list in gui like excel, where on the vertical header should be letters ...aa,ab,ac....dg,dh,di... To do it, I have to declare every place on my list to certain letter. It is probably impossible with yield.

我的意思是,我说,我有100个单元格,并且我想对它们全部命名不同.单元格1应该为"A",单元格2应该为"B"....单元格27应该为"AA",依此类推.您可能是从excel知道的.我可以手动完成,但是要花很多时间.

I mean, let me say, I have 100 of cells and I want to name them all differently. Cell 1 should be "A", Cell 2 should be "B".... Cell 27 should be "AA" and so one. You know it probably from excel. I could do it manually, but it is going to take a lot of time.

好吧,我尝试在下面使用此代码,但没有成功.我知道某个地方应该有一个循环,但是我不知道在哪里.

Well, I tried to play a little with this code underneath, but without success. I know that there should be a loop somewhere, but I have no idea where.

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

范围:对于iter_all_strings()中的s:"在计数直到休息.我会说这应该是我的单元格迭代的循环.只是没有地方.

The scope: "for s in iter_all_strings():" is counting until the break. I would say here should be my loop for iteration for my cells. There's just no place for that.

推荐答案

另一种选择,如果您想更深入地研究(最多创建18,000列):

Another alternative, if you want to dive deeper (create up to ~18,000 columns):

from string import ascii_lowercase

letters = list(ascii_lowercase)
num_cols = 100

excel_cols = []
for i in range(0, num_cols - 1):
    n = i//26
    m = n//26
    i-=n*26
    n-=m*26
    col = letters[m-1]+letters[n-1]+letters[i] if m>0 else letters[n-1]+letters[i] if n>0 else letters[i]
    excel_cols.append(col)

这篇关于python中的连续字母列表并获取其所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆