简体   繁体   English

Python:循环完成后对字母表

[英]Python: Pair alphabets after loop is completed

I tried to pair Alphabets by this 我试图将Alphabets配对

import string
a=string.uppercase
for i in range(0,30):
    print a[i%26]*(i / 26+1)

This will print A-Z and then after Z it will print AA BB like string

but i need to put this string like AA AB AC AD AE until the range is defined after printing AZ then the result will be like print AZ then AA AB AC .... 但我需要把这个字符串像AA AB AC AD AE直到在打印AZ后定义范围,然后结果将像打印AZ然后AA AB AC ....

You can take advantage of the itertools module and use a generator to handle this pretty cleanly: 您可以利用itertools模块并使用生成器来处理这个非常干净:

from itertools import count, product, islice
from string import ascii_uppercase

def multiletters(seq):
    for n in count(1):
        for s in product(seq, repeat=n):
            yield ''.join(s)

gives

>>> list(islice(multiletters('ABC'), 20))
['A', 'B', 'C', 'AA', 'AB', 'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC', 'AAA', 'AAB', 'AAC', 'ABA', 'ABB', 'ABC', 'ACA', 'ACB']
>>> list(islice(multiletters(ascii_uppercase), 30))
['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']

and you can make an object and get them one by one, if you'd prefer: 如果你愿意的话,你可以制作一个物品然后逐个取物:

>>> m = multiletters(ascii_uppercase)
>>> next(m)
'A'
>>> next(m)
'B'
>>> next(m)
'C'

[Update: I should note though that I pass data between Python and Excel all the time -- am about to do so, actually -- and never need this function. [更新:我应该注意,虽然我一直在Python和Excel之间传递数据 - 实际上即将这样做 - 并且永远不需要这个功能。 But if you have a specific question about the best way to exchange data, it's probably better to ask a separate question than to edit this one now that there are several answers to the current question.] 但是,如果你有一个关于交换数据的最佳方式的具体问题,那么现在问一个单独的问题可能比编辑这个问题更好,因为当前问题有几个答案。

I think what you are looking for is a nested for loop, like this: 我认为你要找的是一个嵌套的for循环,如下所示:

import string

def get_string(val):
    return string.uppercase[val%26]*(val / 26+1)


for i in range(0,26):
    for j in range(0, 26):
        print get_string(i) + get_string(j)

Note that I defined your indexing of string.uppercase to a function ( get_string ) so that its code would not be repeated. 请注意,我定义你的索引string.uppercase的功能( get_string ,这样它的代码不会被重复)。

I think what you want is something like this 我想你想要的是这样的

import string

def get_string(val):
    return string.uppercase[val%26]*(val / 26+1)


for i in range(-1,26):
    for j in range(0, 26):
        if i==-1:
            print get_string(j)
        else:
            print get_string(i) + get_string(j)

The first time through the outer loop, do not print a leading character (the first 26 Excel columns) then after that the next 26 columns print a letter followed by a second letter. 第一次通过外部循环,不打印前导字符(前26个Excel列),然后接下来的26列打印一个字母后跟第二个字母。

Working example available on ideone.com -> http://ideone.com/M862Ra 在ideone.com上提供工作示例 - > http://ideone.com/M862Ra

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM