gpt4 book ai didi

python - 尝试使用 itertools.permutations 时出现 MemoryError,如何使用更少的内存?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:22:52 24 4
gpt4 key购买 nike

我正在从一个包含随机字符串的文本文档中加载,并且我正在尝试打印该字符串中字符的所有可能排列。

如果记事本包含例如:

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_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))) 将所有排列存储在内存中然后将所有排列存储在循环中的字符串中,不能保证您能够根据 the_permutation

中的数据量,甚至使用这种方法存储所有数据

如果您只想要一定数量的排列,您可以调用 next 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))

关于python - 尝试使用 itertools.permutations 时出现 MemoryError,如何使用更少的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com