如何增加字母数字值

时间:2014-01-04 20:41:07

标签: python increment urllib

目前我正在制作一个节目。我想要它增加一个5个字符的字母数字值。 (对不起,如果增量不是正确的单词。)

所以我希望程序能够从55aa0开始,到99zz9结束。我希望它从55aa0而不是00aa0开始的原因是因为我正在做的事情是浪费时间。

我还想将该值赋给变量并将其附加到另一个变量的末尾并调用这个url。

例如,url可能是:domain.de/69xh2

如果您需要更多信息,我很乐意添加它。

count = 0
while count <= n:
    url = ""
    if url.endswith(".jpg"):
        fileType = ".jpg"
    elif url.endswith(".png"):
        fileType = ".png"

    if os.path.isfile("images/" + fileName):
        pass
    else:
        urllib.urlretrieve(url, "images/" + count + fileType)
        count = count + 1

3 个答案:

答案 0 :(得分:2)

这听起来像是itertools的工作:

from itertools import dropwhile, islice, product

from string import digits, ascii_lowercase as letters

def values():
    """Yield strings in format '00aa0', starting with '55aa0'."""
    def pred(t):
        """Return False once second digit in tuple t reaches '5'."""
        return t[1] < '5'
    for t in dropwhile(pred, product(digits[5:], digits, letters, 
                                     letters, digits)):
        yield "".join(t)

这开始于(根据Simon的建议使用list(islice(values(), 0, 21))):

['55aa0', '55aa1', '55aa2', '55aa3', '55aa4', '55aa5', 
 '55aa6', '55aa7', '55aa8', '55aa9', '55ab0', '55ab1', 
 '55ab2', '55ab3', '55ab4', '55ab5', '55ab6', '55ab7', 
 '55ab8', '55ab9', '55ac0']

使用itertools的一个优点是你不必在内存中构建整个(304,200元素)列表,但可以迭代它:

for s in values():
    # use s

请注意,此版本与您的要求紧密相关(为了提高效率而向Krab提示),但可以轻松修改以便更具体地使用。

更快的版本,再次来自Krab的建议:

def values():
    """Yield strings in format '00aa0', starting with '55aa0'."""
    for t in product(map(str, range(55, 100)), letters, letters, digits):
        yield "".join(t)

注意:在Python 2.x中使用xrangeitertools.imap

答案 1 :(得分:0)

这取决于你想要首先增加的内容(即结尾处的数字,第一个字母,第二个字母或第一个数字),但我只有单独的变量并将它们连接起来。我还建议从一个数组中调出你的来信:

    letters = ["a","b"..."y","z"]
    var firstNum = 55
    var firstLetter = letters[0]
    var secondLetter = letters[0]
    var scondNum = 0

然后创建一个循环,增加任何你想要的,并连接。例如,如果您想先增加最后一个数字:

varList = []
for i in range(0, 100):
  varList.append(firstNum + firstLetter + secondLetter + (secondNum + i))

然后你会在循环中插入另一个循环,增加第二个字母的索引等等......

希望有所帮助!

答案 2 :(得分:0)

我使用了从右到左逐位添加两个整数的基本算法。

def get_next(st):
    next_str = ""
    increment = '0'*(len(st)-1) + '1'
    index = len(st) -1
    carry = 0
    curr_digit = 0
    while(index>=0):
        if (st[index].isalpha()):

            curr_digit = (ord(st[index]) + int(increment[index]) + carry)
            if curr_digit > ord('z'):
                curr_digit -= ord('a')
                curr_digit %= 26
                curr_digit += ord('a')
                carry = 1
            else:
                carry = 0
            curr_digit = chr(curr_digit)
            next_str += curr_digit

        elif (st[index].isdigit()):
            curr_digit = int(st[index]) + int(increment[index]) + carry
            if curr_digit > 9:
                curr_digit %= 10
                carry = 1
            else:
                carry = 0
            next_str += str(curr_digit)
        index -= 1
    return next_str[::-1]

counter = 20

next_str = '55aa0'
while(counter > 0):
    next_str = get_next(next_str)
    print next_str
    counter -= 1

输出:

55aa1
55aa2
55aa3
55aa4
55aa5
55aa6
55aa7
55aa8
55aa9
55ab0
55ab1
55ab2
55ab3
55ab4
55ab5
55ab6
55ab7
55ab8
55ab9
55ac0