在 Python 中递增字母数字字符串

Nor*_*arz 0 python counter alphanumeric increment

我需要构建一个函数,它获取一个字母数字字符串 (0, 1, ... , 8, 9, A, B, C, ... , Z),加 1 并返回字符串。例如:给定 02H9Z,函数应返回 02HA0。

我在网上找到了几个随机的字母数字字符串生成器。他们工作得很好,但不能解决我的问题。然后我开始编写一个函数,该函数检查 for 循环中的每个字符,并将其与 'A'、'B'、...进行比较 - 但我认为这效率不高。

谁能想到更好的解决方案?

Tig*_*kT3 5

那是基数 36。使用内置int函数和 Numpy 的numpy.base_repr

import numpy
s = '02H9Z'
new = int(s, 36) + 1
print(numpy.base_repr(new, 36))
Run Code Online (Sandbox Code Playgroud)