Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
继续在python中创建可能的字符串列表_Python_String_Itertools - Fatal编程技术网

继续在python中创建可能的字符串列表

继续在python中创建可能的字符串列表,python,string,itertools,Python,String,Itertools,可能重复: 我有这个代码,它生成一个一致的字符串列表 import itertools choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" for length in range(0,20): for entry in itertools.product(choices, repeat = length): string = ''.join(entry) pr

可能重复:

我有这个代码,它生成一个一致的字符串列表

import itertools
choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
for length in range(0,20):
    for entry in itertools.product(choices, repeat = length):
        string = ''.join(entry)
        print string
我希望能够从最后一个已知字符串继续运行此脚本。这怎么可能呢

import itertools
def choices():
    choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
    for length in range(0,20):
        for entry in itertools.product(choices, repeat = length):
            string = ''.join(entry)
            yield string

choice_seq = choices()
print next(choice_seq)
print next(choice_seq)
发电机的意义在于,它们携带着自己的状态


生成器的要点是它们携带状态。

假设您将变量字符串设置为最后一个已知字符串或从开头开始:

import itertools
choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
for length in range(len(string), 20):
    itr = itertools.product(choices, repeat = length)
    if string != '' and length == len(string):
        itr = itertools.dropwhile(tuple(string).__ne__, itr)
    for entry in itr:
        string = ''.join(entry)
        print string
请注意,将打印的第一个元素是最后一个已知的字符串。如果要跳过最后一个已知的字符串并从打印下一个字符串开始,可以在If语句中执行nextir


这假设您正在尝试恢复您在多次执行脚本时中断的位置,或其他生成器解决方案不适用的场景。如果您可以使用生成器,您应该。

假设您将变量字符串设置为最后一个已知字符串或从开头开始:

import itertools
choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
for length in range(len(string), 20):
    itr = itertools.product(choices, repeat = length)
    if string != '' and length == len(string):
        itr = itertools.dropwhile(tuple(string).__ne__, itr)
    for entry in itr:
        string = ''.join(entry)
        print string
请注意,将打印的第一个元素是最后一个已知的字符串。如果要跳过最后一个已知的字符串并从打印下一个字符串开始,可以在If语句中执行nextir


这假设您正在尝试恢复您在多次执行脚本时中断的位置,或其他生成器解决方案不适用的场景。如果您可以使用生成器,您应该。

您保存的状态就是当前长度,以及itertools.product的当前状态。这两样东西都可以腌制。下面是一些伪代码:

import itertools
import pickle

choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"

def tryWithStateSave(currentlength, currentproduct):
    try:
        for entry in currentproduct:
            string = ''.join(entry)
            print string
    except KeyboardInterrupt:
        pickle.dump((currentlength, currentproduct), <saved state file>)
        raise

if <a saved state file exists>:
    currentlength, currentproduct = pickle.load(<the saved state file>)
    tryWithStateSave(currentlength, currentproduct)
    currentlength += 1
else:
    currentlength = 0

for length in range(currentlength+1,20):
    tryWithStateSave(length, itertools.product(choices, repeat = length))

保存的状态仅为当前长度和itertools.product的当前状态。这两样东西都可以腌制。下面是一些伪代码:

import itertools
import pickle

choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"

def tryWithStateSave(currentlength, currentproduct):
    try:
        for entry in currentproduct:
            string = ''.join(entry)
            print string
    except KeyboardInterrupt:
        pickle.dump((currentlength, currentproduct), <saved state file>)
        raise

if <a saved state file exists>:
    currentlength, currentproduct = pickle.load(<the saved state file>)
    tryWithStateSave(currentlength, currentproduct)
    currentlength += 1
else:
    currentlength = 0

for length in range(currentlength+1,20):
    tryWithStateSave(length, itertools.product(choices, repeat = length))
这可能是相关的:这可能是相关的: