The 文档 http://docs.python.org/library/itertools.html#itertools.product表示笛卡尔积函数
the actual implementation does not build up intermediate results in memory.
这怎么可能用发电机实现呢?有人可以给我举个例子吗
2 个生成器的内存消耗有限?
查看模块的源代码,itertools.product()
实际上将每个参数转换为一个元组:
// product_new() in itertoolsmodule.c
for (i=0; i < nargs ; ++i) {
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *pool = PySequence_Tuple(item); //<==== Call tuple(arg)
if (pool == NULL)
goto error;
PyTuple_SET_ITEM(pools, i, pool);
indices[i] = 0;
}
换句话说,itertools.product()
的内存消耗似乎与输入参数的大小呈线性关系。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)