2017-10-11 43 views
0

我正在尋找一個片斷它不喜歡的東西:Python2增量串

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

無法找到一個方法來做到這一點:(

我已經嘗試過的東西一樣使用'aaaa'.encode(' 十六進制「),然後增加它,但沒有工作:(

感謝您的幫助,

乾杯!

回答

0

這是一種方法:

# 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 
+0

謝謝!!工作正常 :) –