[英]Why does the following python code throw a Memory error?
Here is the code:这是代码:
import itertools
num_cases = int(input())
answer_list = []
while num_cases>0:
live_ans = []
question_list = []
nums = int(input())
d1 = int(input())
d2 = int(input())
sample_space= {d1, d2}
temp = []
no_cases = 2**(nums-1)
combs = itertools.product(sample_space, repeat = nums-1)
for i in combs:
temp.append(i)
for i in temp:
if sum(i) not in live_ans:
live_ans.append(sum(i))
else:
pass
live_ans.sort()
answer_list.append(live_ans)
num_cases -= 1
for i in answer_list:
finalans = " ".join(map(str, i))
print(finalans)
For small inputs like:对于小输入,如:
1
3
1
2
The program works just fine.该程序运行良好。 Where as for a relatively larger input like:
至于相对较大的输入,如:
1
58
69
24
It gives a memory error.它给出了一个内存错误。 I dont cite any reason for this as the code doesn't look memory consuming at all.
我没有为此引用任何原因,因为代码看起来根本不消耗内存。 Isn't it?
不是吗?
Look at your following lines:看看你的以下几行:
no_cases = 2**(nums-1)
combs = itertools.product(sample_space, repeat = nums-1)
for i in combs:
temp.append(i)
2**58 = 2.8823038e+17 2**58 = 2.8823038e+17
You do the math on why it gets a memory error from here你计算一下为什么它会从这里得到内存错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.