笛卡尔积内存错误,将itertools.product转换为列表 [英] Cartesian Product memory error converting itertools.product to list

查看:91
本文介绍了笛卡尔积内存错误,将itertools.product转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建列表列表的笛卡尔积.当我尝试将结果转换为列表时,它将给我一个内存错误.如果我在不将其转换为列表的情况下运行它,则运行正常.

I'm trying to create the Cartesian product of a list of lists. When I try to convert the result to a list it will give me a memory error. If I run it without converting it to a list it runs fine.

lists = [['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ]]
my_product = list(itertools.product(*lists))

我什至尝试使用itertools.dropwhile筛选某些结果,以使其变小,然后再将其转换为列表,然后得到相同的结果.

I even tried to filter through some of the result using itertools.dropwhile to make it smaller before converting it into a list and I get the same result.

filtered = itertools.dropwhile(lambda x: x[1]!=x[2] and x[3]!=x[4] and x[3]!=x[5] and x[4]!=x[5], my_product)

推荐答案

您正在创建19683个由9个元素组成的新元组.您的计算机一次没有足够的内存来存储所有这些元组.

You are creating 19683 new tuples of 9 elements. Your computer does not have enough memory available for all those tuples at once.

如果不使用list(),则仅创建一个生成器对象,当您对其进行迭代时,该生成器将逐一生成19683.

If you do not use list(), then only a generator object is created, one that will produce the 19683 one-by-one as you iterate over it.

您应该过滤itertools.product() 的输出,而无需将其转换为列表:

You should filter the output of itertools.product() without converting it to a list:

my_product = itertools.product(*lists)
filtered = itertools.dropwhile(lambda x: x[1]!=x[2] and x[3]!=x[4] and x[3]!=x[5] and x[4]!=x[5], my_product)

您可以将filtered转换为列表,但是如果您要做的只是遍历该列表并逐项处理项目,则不要这样做.仅在需要随机对元素的访问权限时,才将其变成一个列表.

You could turn filtered into a list, but if all you do is loop over that list and process items one by one, you should not do that. Only turn it into a list if you need random access to the elements.

这篇关于笛卡尔积内存错误,将itertools.product转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆