itertool.islice 停止值大于 sys.maxsize 时的值错误
itertool.islice value error when stop value is greater than sys.maxsize
itertools.islice()
方法让我可以从给定值开始的字符集中生成字符组合。 运行 我的 Generate(1, 7, "abcde", "bdca")
方法 运行 非常好。
但是,当整数处于 'maximum' 值(大于 2147483647
)时,我得到错误:
ValueError: Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.
我怎样才能itertools.islice
获取较大的起始值?
我确实尝试将 sys.maxsize 设置为 'a large number' 并将 startValue
显式转换为整数; sys.maxsize = (len(charset) ** maxVal)
,但 islice()
只是忽略了这一点。
这是我到目前为止想出的代码:
def checkValue(charset, word):
pos = len(charset)
value = 0
for i,c in enumerate(reversed(word)):
value+= (pos**i) * charset.index(c)
return value
def Generate(minVal, maxVal, charset, startFrom):
startValue = int(checkValue(charset, startFrom))
print(startValue)
allCombos = itertools.product(charset, repeat=len(startFrom))
combos = itertools.islice(allCombos, int(startValue), None) # error is here with 'startValue'
# generate from combo to end of length
for num, attempt in enumerate(combos, start=startValue):
generated = "".join(attempt)
print(num, generated)
# have to make new instance or skips a chunk for each length
for length in range(minVal + 1, maxVal + 1):
to_attempt = itertools.product(charset, repeat=length)
for attempt in to_attempt:
generated = "".join(attempt)
print(generated)
Generate(1, 15, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890", "ADHkjdWCE")
感谢您的帮助。
这是 islice
的一个实现细节,如果不手动重新实现就无法直接解决。
如果您移动到 Python 的 64 位版本,sys.maxsize
将从 2**31 - 1
跳到 2**63 - 1
,它太大了,实际上 运行在任何人类合理的时间内完成这么长的切片。
注意:你这里的设计是个坏主意。 islice
不是魔法;它仍然必须 运行 出来(丢弃结果)才能到达 startValue
。这样做 2+ 十亿次将花费很长时间。我建议找到一种方法在以后直接开始迭代,而不是从头开始并丢弃 2+ 十亿个项目。
itertools.islice()
方法让我可以从给定值开始的字符集中生成字符组合。 运行 我的 Generate(1, 7, "abcde", "bdca")
方法 运行 非常好。
但是,当整数处于 'maximum' 值(大于 2147483647
)时,我得到错误:
ValueError: Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.
我怎样才能itertools.islice
获取较大的起始值?
我确实尝试将 sys.maxsize 设置为 'a large number' 并将 startValue
显式转换为整数; sys.maxsize = (len(charset) ** maxVal)
,但 islice()
只是忽略了这一点。
这是我到目前为止想出的代码:
def checkValue(charset, word):
pos = len(charset)
value = 0
for i,c in enumerate(reversed(word)):
value+= (pos**i) * charset.index(c)
return value
def Generate(minVal, maxVal, charset, startFrom):
startValue = int(checkValue(charset, startFrom))
print(startValue)
allCombos = itertools.product(charset, repeat=len(startFrom))
combos = itertools.islice(allCombos, int(startValue), None) # error is here with 'startValue'
# generate from combo to end of length
for num, attempt in enumerate(combos, start=startValue):
generated = "".join(attempt)
print(num, generated)
# have to make new instance or skips a chunk for each length
for length in range(minVal + 1, maxVal + 1):
to_attempt = itertools.product(charset, repeat=length)
for attempt in to_attempt:
generated = "".join(attempt)
print(generated)
Generate(1, 15, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890", "ADHkjdWCE")
感谢您的帮助。
这是 islice
的一个实现细节,如果不手动重新实现就无法直接解决。
如果您移动到 Python 的 64 位版本,sys.maxsize
将从 2**31 - 1
跳到 2**63 - 1
,它太大了,实际上 运行在任何人类合理的时间内完成这么长的切片。
注意:你这里的设计是个坏主意。 islice
不是魔法;它仍然必须 运行 出来(丢弃结果)才能到达 startValue
。这样做 2+ 十亿次将花费很长时间。我建议找到一种方法在以后直接开始迭代,而不是从头开始并丢弃 2+ 十亿个项目。