Python MemoryError在尝试使用itertools.permutations时,如何使用更少的内存?
我从一个包含随机字符串的文本文档中加载,并试图打印该字符串中所有可能的字符排列 如果记事本包含以下内容,例如:Python MemoryError在尝试使用itertools.permutations时,如何使用更少的内存?,python,algorithm,python-3.x,permutation,itertools,Python,Algorithm,Python 3.x,Permutation,Itertools,我从一个包含随机字符串的文本文档中加载,并试图打印该字符串中所有可能的字符排列 如果记事本包含以下内容,例如: 123 abc 我希望我的输出是 123,132,213,231,312,321 abc,acb,bac,bca,cab,cba 文本文件包含一些相当大的字符串,因此我可以了解为什么会出现此MemoryError 我的第一次尝试是这样: import sys import itertools import math def organize(to_print): numb
123
abc
我希望我的输出是
123,132,213,231,312,321
abc,acb,bac,bca,cab,cba
文本文件包含一些相当大的字符串,因此我可以了解为什么会出现此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
,我需要在开始排列之前按数字、大小写对其进行预排序
我在这里得到了内存错误:complete\u string=''.join((list(itertools.permutations(organize(number)))[x])
,所以我最初的尝试是将它从for循环中带出来
我的第二次尝试是:
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)
但是我仍然有一个记忆错误。我不需要或者不想直接得到答案,因为这是一个很好的学习实践,可以降低我的效率,所以在正确的方向上给出提示会很好
增加了第三次尝试:
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)
不要调用列表,只需迭代排列:
the_permutation = itertools.permutations(organize(number))
for x in the_permutation:
complete_string = ''.join(the_permutation)
list(itertools.permutations(organize(number))
将所有排列存储在内存中,然后将所有排列存储在循环中的字符串中,根据排列中的数据量,无法保证即使使用这种方法也能存储所有数据
如果您只需要一定数量的排列,您可以调用下一个om排列对象:
the_permutation = itertools.permutations(organize(number))
for x in range(factorial):
complete_string = ''.join(next(the_permutation))
或者使用itertools.islice:
for x in itertools.islice(the_permutation,factorial):
complete_string = ''.join(next(the_permutation))
不要调用列表,只需迭代排列:
the_permutation = itertools.permutations(organize(number))
for x in the_permutation:
complete_string = ''.join(the_permutation)
list(itertools.permutations(organize(number))
将所有排列存储在内存中,然后将所有排列存储在循环中的字符串中,根据排列中的数据量,无法保证即使使用这种方法也能存储所有数据
如果您只需要一定数量的排列,您可以调用下一个om排列对象:
the_permutation = itertools.permutations(organize(number))
for x in range(factorial):
complete_string = ''.join(next(the_permutation))
或者使用itertools.islice:
for x in itertools.islice(the_permutation,factorial):
complete_string = ''.join(next(the_permutation))
请记住,阶乘增长非常快
。。。因此,即使对于中等长度的字符串,排列的数量也是巨大的。12个字母大约有4.8亿个。记住,阶乘增长非常快
。。。因此,即使对于中等长度的字符串,排列的数量也是巨大的。对于12个字母,大约有4.8亿个。您正在将所有可能的排列组合分解成一个列表。你确定你需要随机访问所有可能的排列吗?我认为排列的数量比你想象的要多。假设您有一个a4的文本。然后,在所有的排列中,有所有可能的英文、德文和法文诗歌……你正在将所有可能的排列展开成一个列表。你确定你需要随机访问所有可能的排列吗?我认为排列的数量比你想象的要多。假设您有一个a4的文本。然后在所有的排列中都有可能用英语、德语和法语写的诗……在考虑列表可能是问题之前,我试过这样做,这里是我的错误:
Traceback(最后一次调用):文件“question.py”,第27行,完整的字符串='''.join(排列)TypeError:sequence item 0:expected str instance,tuple found
我将尝试看看我可以在哪里使用它。这是因为您正在尝试连接元组,迭代然后连接每个元组与在排列对象上调用连接不同。如果我得到这个结果,这意味着什么Traceback(最近一次调用):File“question.py”,第27行,在complete_string=''.join(the_permutation)TypeError:sequence item 0:expected str instance,tuple found
中,我将尝试看看我可以在哪里使用它。这是因为您正在尝试连接tuple,迭代然后连接每个元组与在排列对象上调用连接不同,如果我得到这个结果,这意味着什么