【问题标题】:Python2 increment string in an alphanumeric formatPython2以字母数字格式递增字符串
【发布时间】:2017-10-11 21:22:44
【问题描述】:

我正在寻找一个可以执行以下操作的 sn-p:

from 'abcd8' string
i want to increment it like 'abcd9' then 'abcda' ... >'abcdz' and next     'abce0', 'abce1' ... 'abcez', 'abcf0' ....

找不到办法:(

我已经尝试过使用 'aaaa'.encode('hex') 然后递增它,但不起作用:(

谢谢你的帮助,

干杯!

【问题讨论】:

    标签: string python-2.7 alphanumeric


    【解决方案1】:

    这是一种方法:

    # written by 'imerso' to a StackOverflow answer
    def increment(os):
        s = list(os)
        p = len(os)-1
    
        while (p >= 0):
            s[p] = chr(ord(s[p]) + 1)
    
            if s[p] > 'z':
                s[p] = '0'
                p -= 1
                continue
    
            if s[p] > '9' and s[p] < 'a':
                s[p] = 'a'
    
            return ''.join(s)
    
    
    s = raw_input("String: " )
    
    for x in range(1, 32):
        s = increment(s)
        print s
    

    【讨论】:

    • 谢谢!!工作正常:)
    猜你喜欢
    • 2014-10-08
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2011-07-21
    • 2012-04-22
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多