Generate alphanumeric strings sequentially

前端 未结 5 2056
我寻月下人不归
我寻月下人不归 2020-12-25 08:38

I\'m trying to create a loop to generate and print strings as follows:

  1. Alphanumeric characters only:
  2. 0-9 are before A-Z, which are before a-z,
5条回答
  •  梦毁少年i
    2020-12-25 09:30

    from string import digits, ascii_uppercase, ascii_lowercase
    from itertools import product
    chars = digits + ascii_uppercase + ascii_lowercase
    
    def give_me_next(lst):
            lst = lst[::-1]
            change_next = False
            change = True
            n = 0
            for x in lst:
                    if change_next == True:
                            change_next = False
                            pos = chars.find(x)
                            try:
                                    a =  chars[pos+1]
                                    lst = list(lst)
                                    lst[n] = a
                                    lst = "".join(lst)
                                    x = a
                            except:
                                    lst = list(lst)
                                    lst[n] = '0'
                                    lst = "".join(lst)
                                    change_next = True
                                    x = '0'
    
                    pos = chars.find(x)
                    try:
                            a =  chars[pos+1]
                            if change == True:
                                    lst = list(lst)
                                    lst[n] = a
                                    lst = "".join(lst)
                                    change = False
                    except:
                            lst = list(lst)
                            lst[n] = '0'
                            lst = "".join(lst)
                            change_next = True
    
                    n = n + 1
    
            lst = lst[::-1]
            return lst
    
    a=  give_me_next('zzzzz')
    while True:
            a =  give_me_next(a)
            print a
    

提交回复
热议问题