<generator object =“”product =“”at =“”0x000000000315c438 =“”> </generator>

时间:2013-11-27 20:11:58

标签: python python-3.x

我收到此错误?我不知道它是什么以及它为什么会来。

这是我的代码:

def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
    result = [x+[y] for x in result for y in pool]
for prod in result:
    yield tuple(prod)

1 个答案:

答案 0 :(得分:4)

这不是错误。相反,它是product创建的生成器对象的对象ID。

如果您希望将结果作为列表,请将函数调用放在list

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)
###############################
myproduct = list(product(...))
###############################
print (myproduct)

以下是基本演示:

>>> def func():
...     for i in range(10):
...         yield i
...
>>> func()
<generator object func at 0x01ADA210>
>>> list(func())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
相关问题