将起始索引添加到字符串索引生成器

时间:2017-09-22 09:41:31

标签: python string generator itertools

我目前正在学习创建生成器并使用itertools。所以我决定创建一个字符串索引生成器,但我想添加一些参数,例如“开始索引”,允许定义从哪里开始生成索引。

我想出了这个丑陋的解决方案,这个解决方案可能很长而且对于大型索引效率不高:

import itertools
import string

class StringIndex(object):
    '''
    Generator that create string indexes in form:
    A, B, C ... Z, AA, AB, AC ... ZZ, AAA, AAB, etc.

    Arguments:
    - startIndex = string; default = ''; start increment for the generator.
    - mode = 'lower' or 'upper'; default = 'upper'; is the output index in
      lower or upper case.
    '''

    def __init__(self, startIndex = '', mode = 'upper'):

        if mode == 'lower':
            self.letters = string.ascii_lowercase

        elif mode == 'upper':
            self.letters = string.ascii_uppercase

        else:
            cmds.error ('Wrong output mode, expected "lower" or "upper", ' + 
                        'got {}'.format(mode))

        if startIndex != '':
            if not all(i in self.letters for i in startIndex):
                cmds.error ('Illegal characters in start index; allowed ' + 
                            'characters are: {}'.format(self.letters))

        self.startIndex = startIndex


    def getIndex(self):
        '''
        Returns:
        - string; current string index
        '''
        startIndexOk = False
        x = 1
        while True:
            strIdMaker = itertools.product(self.letters, repeat = x)

            for stringList in strIdMaker:
                index = ''.join([s for s in stringList])

                # Here is the part to simpify
                if self.startIndex:
                    if index == self.startIndex:
                        startIndexOk = True

                    if not startIndexOk:
                        continue
                ###

                yield index
            x += 1

欢迎任何建议或改进。谢谢!

修改
起始索引必须是一个字符串!

2 个答案:

答案 0 :(得分:1)

您必须自己进行算术运算(基数为26),以避免在itertools.product上进行循环。但你至少可以设置x=len(self.startIndex) or 1

答案 1 :(得分:0)

旧(不正确)回答

如果您不使用itertools(假设您以单个字母开头),则可以执行以下操作:

letters = 'abcdefghijklmnopqrstuvwxyz'
def getIndex(start, case):
    lets = list(letters.lower()) if case == 'lower' else list(letters.upper())
    # default is 'upper', but can also be an elif

    for r in xrange(0,10):
        for l in lets[start:]:
            if l.lower() == 'z':
                start = 0
            yield ''.join(lets[:r])+l

我一直运行,直到创建了最多10行字母,但你可以使用无限循环,这样就可以永久地调用它。

正确答案

我以不同的方式找到了解决方案:我使用了基数为26的数字转换器(基于(并且因为它不能完美地工作而修复):http://quora.com/How-do-I-write-a-program-in-Python-that-can-convert-an-integer-from-one-base-to-another

我使用itertools.count()来计算并循环所有可能性。

代码:

import time
from itertools import count

def toAlph(x, letters):
    div = 26
    r = '' if x > 0 else letters[0]
    while x > 0:
        r = letters[x % div] + r
        if (x // div == 1) and (x % div == 0):
            r = letters[0] + r
            break
        else:
            x //= div
    return r

def getIndex(start, case='upper'):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    letters = alphabet.upper() if case == 'upper' else alphabet

    started = False
    for num in count(0,1):
        l = toAlph(num, letters)
        if l == start:
            started = True

        if started:
            yield l

iterator = getIndex('AA')
for i in iterator:
    print(i)
    time.sleep(0.1)