尝试使用itertools.permutations时出现MemoryError,如何使用较少的内存? [英] MemoryError while trying to using itertools.permutations, how use less memory?
问题描述
我正在从包含如此随机字符串的文本文档中加载文件,并且尝试打印该字符串中所有可能的字符排列.
I'm loading from a text document containing so random strings and I'm trying to print every possible permutation of the characters in that string.
如果记事本包含例如:
123
abc
我希望输出为
123,132,213,231,312,321
abc,acb,bac,bca,cab,cba
该文本文件包含一些相当大的字符串,因此我可以看到为什么收到此MemoryError.
The text file contains some pretty large strings so I can see why I am getting this MemoryError.
我第一次尝试使用它:
import sys
import itertools
import math
def organize(to_print):
number_list = []
upper_list = []
lower_list = []
for x in range(0,len(to_print)):
if str(to_print[x]).isdigit() is True:
number_list.append(to_print[x])
elif to_print[x].isupper() is True:
upper_list.append(to_print[x])
else:
lower_list.append(to_print[x])
master_list = number_list + upper_list + lower_list
return master_list
number = open(*file_dir*, 'r').readlines()
factorial = math.factorial(len(number))
complete_series = ''
for x in range(0,factorial):
complete_string = ''.join((list(itertools.permutations(organize(number)))[x]))
complete_series += complete_string+','
edit_series = complete_series[:-1]
print(edit_series)
def organize
的原因是,如果我有一个字符串1aB
,则在开始排列之前,我需要按数字,大写,小写对其进行排序.
The reason for def organize
is if I have a string 1aB
I would need to preorder it by number,uppercase,lowercase before I start the permutations.
我在这里遇到内存错误:complete_string = ''.join((list(itertools.permutations(organize(number)))[x]))
,所以我最初的尝试是将其带出for循环.
I got the memory error here: complete_string = ''.join((list(itertools.permutations(organize(number)))[x]))
so my initial attempt was to bring it out of the for-loop.
我的第二次尝试是
import sys
import itertools
import math
def organize(to_print):
number_list = []
upper_list = []
lower_list = []
for x in range(0,len(to_print)):
if str(to_print[x]).isdigit() is True:
number_list.append(to_print[x])
elif to_print[x].isupper() is True:
upper_list.append(to_print[x])
else:
lower_list.append(to_print[x])
master_list = number_list + upper_list + lower_list
return master_list
number = open(*file_dir*, 'r').readlines()
factorial = math.factorial(len(number))
complete_series = ''
the_permutation = list(itertools.permutations(organize(number)))
for x in range(0,factorial):
complete_string = ''.join((the_permutation[x]))
complete_series += complete_string+','
edit_series = complete_series[:-1]
print(edit_series)
但是我仍然遇到内存错误.我不一定需要或直接想要答案,因为这是减少我的低效率的良好学习习惯,因此朝正确方向的提示会很好.
But I am still getting a memory error. I don't necessarily need or want the answer directly as this is good learning practice to reduce my inefficiencies, so hints in the right direction would be nice.
添加了第三次尝试:
import sys
import itertools
import math
def organize(to_print):
number_list = []
upper_list = []
lower_list = []
for x in range(0,len(to_print)):
if str(to_print[x]).isdigit() is True:
number_list.append(to_print[x])
elif to_print[x].isupper() is True:
upper_list.append(to_print[x])
else:
lower_list.append(to_print[x])
master_list = number_list + upper_list + lower_list
return master_list
number = open(*file_dir*, 'r').readlines()
factorial = math.factorial(len(number))
complete_series = ''
the_permutation = itertools.permutations(organize(number))
for x in itertools.islice(the_permutation,factorial):
complete_string = ''.join(next(the_permutation))
complete_series += complete_string+','
edit_series = complete_series[:-1]
print(edit_series)
推荐答案
不调用列表,只需遍历排列即可:
Don't call list, just iterate over the permutations:
the_permutation = itertools.permutations(organize(number))
for x in the_permutation:
complete_string = ''.join(the_permutation)
list(itertools.permutations(organize(number)))
将所有排列存储在内存中,然后将所有排列存储在循环中的字符串中,即使使用此方法,也无法保证您将能够存储所有数据,具体取决于所存储的数据量the_permutation
list(itertools.permutations(organize(number)))
stores all the permutations in memory then you store all the permutations in a string in your loop, there is no guarantee that you will be able to store all the data even using this approach depending on how much data is in the_permutation
如果只需要一定数量的置换,则可以在置换对象中调用下一个:
If you only want a certain amount of the permutations you can call next om the permutations object:
the_permutation = itertools.permutations(organize(number))
for x in range(factorial):
complete_string = ''.join(next(the_permutation))
或使用itertools.islice:
Or use itertools.islice:
for x in itertools.islice(the_permutation,factorial):
complete_string = ''.join(next(the_permutation))
这篇关于尝试使用itertools.permutations时出现MemoryError,如何使用较少的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!