Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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上的itertools,然后再继续吗?_Python_Performance_Permutation_Itertools - Fatal编程技术网

我可以暂停python上的itertools,然后再继续吗?

我可以暂停python上的itertools,然后再继续吗?,python,performance,permutation,itertools,Python,Performance,Permutation,Itertools,我需要创建一个字符串列表,包含所有字母大小写的所有可能组合,以及长度为14的非重复字符,这是一个庞大的列表,我知道这将占用大量的时间和空间。 我现在的代码是: import itertools filename = open("strings.txt", "w") for com in itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14):

我需要创建一个字符串列表,包含所有字母大小写的所有可能组合,以及长度为14的非重复字符,这是一个庞大的列表,我知道这将占用大量的时间和空间。 我现在的代码是:

import itertools

filename = open("strings.txt", "w")

for com in itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14):
    filename.write("\n"+"1_"+"".join(com)+"\n"+"0_"+"".join(com))
    print ("".join(com))
非常基本,它完成了这项工作,但我还没有找到更快的方法(尝试了一种java算法,我发现它似乎更快,但python更快) 由于这需要很长时间,我需要不时地关闭电脑,因此我需要能够保存我离开的地方并继续,否则每次它崩溃/关闭电脑/发生任何事情时,我都会从头开始。 有什么方法可以做到这一点吗?

您可以使用该迭代器对象。其内部状态将存储在pickle文件中。当你恢复时,它应该从它停止的地方开始

大概是这样的:

import itertools
import os
import pickle
import time

# if the iterator was saved, load it
if os.path.exists('saved_iter.pkl'):
    with open('saved_iter.pkl', 'rb') as f:
        iterator = pickle.load(f)
# otherwise recreate it
else:
    iterator = itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14)

try:
    for com in iterator:
        # process the object from the iterator
        print(com)
        time.sleep(1.0)
except KeyboardInterrupt:
    # if the script is about to exit, save the iterator state
    with open('saved_iter.pkl', 'wb') as f:
        pickle.dump(iterator, f)
其结果是:

>python so_test.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'p')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'q')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'r')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 's')

>python so_test.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 't')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'u')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'v')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'w')

为什么需要计算所有这些排列?下一步你会怎么做?每次迭代写入文件都很慢,我建议定期写入文件,例如每1000或10000次迭代。嗯,我不确定你是否真的知道这需要多长时间。大约有1.54x10^23个排列。即使每次迭代只需要1纳秒(比那要长得多),它仍然需要1.54x10^14秒,也就是说,输出文件就在附近。我知道所有这些。我会做更多的限制,以减少痛苦,但我没有问这是否可行,可行性等,我要求暂停Lol他在打开文件时也应该使用附加选项
“strings.txt”
,否则之前的结果将被覆盖。对于“strings.txt”文件,是的。为了简单起见,我在答案中省略了这一点。这看起来很有趣!非常感谢。是的,我想我应该在暂停后将模式改为append