在Python中递增字母数字字符串
我需要构建一个函数,它获取一个字母数字字符串(0,1,…,8,9,a,B,C,…,Z),添加1并返回字符串。例如:给定02H9Z时,函数应返回02HA0 我在网上发现了几个随机字母数字字符串生成器。它们工作得很好,但不能解决我的问题。然后我开始编写一个函数,它检查for循环中的每个字符,并将其与“a”、“B”…-但我认为这效率不高在Python中递增字母数字字符串,python,counter,increment,alphanumeric,Python,Counter,Increment,Alphanumeric,我需要构建一个函数,它获取一个字母数字字符串(0,1,…,8,9,a,B,C,…,Z),添加1并返回字符串。例如:给定02H9Z时,函数应返回02HA0 我在网上发现了几个随机字母数字字符串生成器。它们工作得很好,但不能解决我的问题。然后我开始编写一个函数,它检查for循环中的每个字符,并将其与“a”、“B”…-但我认为这效率不高 有谁能想出更好的解决办法吗 这是基数36。使用内置的int功能和Numpy的Numpy.base\u repr: import numpy s = '02H9Z' n
有谁能想出更好的解决办法吗 这是基数36。使用内置的
int
功能和Numpy的Numpy.base\u repr
:
import numpy
s = '02H9Z'
new = int(s, 36) + 1
print(numpy.base_repr(new, 36))
这是基数36。使用内置的
int
功能和Numpy的Numpy.base\u repr
:
import numpy
s = '02H9Z'
new = int(s, 36) + 1
print(numpy.base_repr(new, 36))
以下是仅使用内置函数的解决方案:
l = '0123456789abcdefghijklmnopqrstuvwxyz'
def increase(s):
new_s = []
continue_change = True
for c in s[::-1].lower():
if continue_change:
if c == 'z':
new_s.insert(0, '0')
else:
new_s.insert(0, l[l.index(c) + 1])
continue_change = False
else:
new_s.insert(0, c)
return ''.join(new_s)
以下是仅使用内置函数的解决方案:
l = '0123456789abcdefghijklmnopqrstuvwxyz'
def increase(s):
new_s = []
continue_change = True
for c in s[::-1].lower():
if continue_change:
if c == 'z':
new_s.insert(0, '0')
else:
new_s.insert(0, l[l.index(c) + 1])
continue_change = False
else:
new_s.insert(0, c)
return ''.join(new_s)
我认为它是十六进制(或任何基数),而不是尝试增加字符串。因此,您想使用bas 36进行计算?我认为它可能是十六进制(或任何基数)的副本而不是试图增加一个字符串。所以你想用bas 36进行计算吗?可能是重复的该死的我快发帖了。该死的我快发帖了。