如何在给定起点和终点之间生成 python 中的字符串序列
how to generate sequence of string in python between given start and end point
我想输入作为开始输入,例如 22Ef
和 FFFF
作为结束输入,我想打印这两个输入之间的所有字符串,输出应该是这样的
22Ef
22EA
22EB
.
.
.
FFFE
FFFF
这是我要选择字符的字符串
0123456789abcdefABCDEF
以下(使用 itertools
中的 product
函数)应该有效:
from itertools import product
strings = []
start = "22Ef"
end = "FFFF"
include = False
for i in product("0123456789abcdefABCDEF", repeat=4):
curstring = "".join(i)
if curstring == start:
include = True
if curstring == end:
strings.append(curstring)
break
if include:
strings.append(curstring)
print(strings)
此解决方案避免遍历从 0000 到您的起始输入的所有组合:
start = '22Ef'
end = 'FFFF'
characterset = '0123456789abcdefABCDEF'
base = len(characterset)
baseNnumber = [] # little endian list of integers each representing a digit
# find base N representation of start
exponent = 0
startval = 0
for i in start[::-1]:
digit = characterset.index(i)
baseNnumber.append(digit)
startval += digit * base**exponent
exponent += 1
baseNnumber.extend([0]*(len(end)-len(start)))
# find representation of end
exponent = 0
endval = 0
for i in end[::-1]:
digit = characterset.index(i)
endval += digit * base**exponent
exponent += 1
# number of times to print
increments = endval - startval + 1
for i in range(increments):
index = 0
outstr = ''
# remove leading zeros
notzero = False
for i in baseNnumber[::-1]:
notzero = notzero | (i != 0)
# add new character to string
outstr += characterset[i] * notzero
# the actual printing
print(outstr)
# increment baseNnumber by 1
while True:
baseNnumber[index] += 1
if baseNnumber[index] == base:
baseNnumber[index] = 0
index += 1
else:
break
如果您希望包含前导零,您可能需要用此替换第 34-38 行,因为 notzero
旨在删除它们:
for i in baseNnumber[::-1]:
outstr += characterset[i]
或者如果您确定开始和结束输入相同,只需删除第 17 行并进行上面的替换,因为它旨在解决开始和结束输入长度不同的问题。
我想输入作为开始输入,例如 22Ef
和 FFFF
作为结束输入,我想打印这两个输入之间的所有字符串,输出应该是这样的
22Ef
22EA
22EB
.
.
.
FFFE
FFFF
这是我要选择字符的字符串
0123456789abcdefABCDEF
以下(使用 itertools
中的 product
函数)应该有效:
from itertools import product
strings = []
start = "22Ef"
end = "FFFF"
include = False
for i in product("0123456789abcdefABCDEF", repeat=4):
curstring = "".join(i)
if curstring == start:
include = True
if curstring == end:
strings.append(curstring)
break
if include:
strings.append(curstring)
print(strings)
此解决方案避免遍历从 0000 到您的起始输入的所有组合:
start = '22Ef'
end = 'FFFF'
characterset = '0123456789abcdefABCDEF'
base = len(characterset)
baseNnumber = [] # little endian list of integers each representing a digit
# find base N representation of start
exponent = 0
startval = 0
for i in start[::-1]:
digit = characterset.index(i)
baseNnumber.append(digit)
startval += digit * base**exponent
exponent += 1
baseNnumber.extend([0]*(len(end)-len(start)))
# find representation of end
exponent = 0
endval = 0
for i in end[::-1]:
digit = characterset.index(i)
endval += digit * base**exponent
exponent += 1
# number of times to print
increments = endval - startval + 1
for i in range(increments):
index = 0
outstr = ''
# remove leading zeros
notzero = False
for i in baseNnumber[::-1]:
notzero = notzero | (i != 0)
# add new character to string
outstr += characterset[i] * notzero
# the actual printing
print(outstr)
# increment baseNnumber by 1
while True:
baseNnumber[index] += 1
if baseNnumber[index] == base:
baseNnumber[index] = 0
index += 1
else:
break
如果您希望包含前导零,您可能需要用此替换第 34-38 行,因为 notzero
旨在删除它们:
for i in baseNnumber[::-1]:
outstr += characterset[i]
或者如果您确定开始和结束输入相同,只需删除第 17 行并进行上面的替换,因为它旨在解决开始和结束输入长度不同的问题。