Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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.permutations进程_Python_Memory Management_Itertools - Fatal编程技术网

在Python中细分itertools.permutations进程

在Python中细分itertools.permutations进程,python,memory-management,itertools,Python,Memory Management,Itertools,有没有办法对占用大量内存的进程进行细分(本例中是itertools.permutations),以提高效率并避免内存不足?您已经用itertools.permutations将自己逼入困境。你真的需要检查所有可能的排列吗?您的字典非常小,因此只需迭代字典本身并验证每个单词: import itertools from collections import Counter with open('/usr/share/dict/american-english', 'r') as handle:

有没有办法对占用大量内存的进程进行细分(本例中是itertools.permutations),以提高效率并避免内存不足?

您已经用
itertools.permutations
将自己逼入困境。你真的需要检查所有可能的排列吗?您的字典非常小,因此只需迭代字典本身并验证每个单词:

import itertools
from collections import Counter

with open('/usr/share/dict/american-english', 'r') as handle:
    dictionary = frozenset(line.strip() for line in handle)

def existing_words(letters, length):
    letters_counter = Counter(letters)
    letters_set = frozenset(letters)

    for word in dictionary:
        if len(word) != length:
            continue

        if set(word) <= letters_set and Counter(word) <= letters_counter:
            yield word

if __name__ == '__main__':
    for word in existing_words('abcdefghijklmnopqrst', 5):
        print word
导入itertools
从收款进口柜台
以open('/usr/share/dict/american english,'r')作为手柄:
dictionary=frozenset(line.strip()表示句柄中的行)
定义现有单词(字母、长度):
字母\计数器=计数器(字母)
字母集=冻结集(字母)
对于字典中的单词:
如果len(word)!=长度:
持续

如果设置(word),为什么要一次存储所有排列?@Blender将它们转换成一个列表(python列表),然后与另一个列表进行比较。您不需要这样做:只需执行
all(a==b表示a,b表示zip中的b(其他一个,排列(…))
,并且您有一个惰性的相等检查,一次只使用一个元素的内存。(好吧,几乎是这样,因为它检查前缀,而不是真正的相等。但是你可以使用
zip\u longest
)修复它。)@user3797646:“比较”是什么意思?你能准确地解释一下你想做什么吗?@Blender:这里有一篇帖子解释了我想做什么(看看可能存在的单词()函数):