我想幫助與卡爾Knechtel(12月13日'10在7註釋的理解:32)。下面的代碼演示瞭如何使用發電機,原拉姆達的定義給出了預期的結果,但它並沒有使用列表或元組:
>>> #GENERATOR
... basis = ((lambda x: n*x) for n in [0, 1, 2])
>>> print(type(basis))
<type 'generator'>
>>> basis = ((lambda x: n*x) for n in [0, 1, 2])
>>> print([x(3) for x in basis])
[0, 3, 6]
>>> #TUPLE
... basis = tuple((lambda x: n*x) for n in [0, 1, 2])
>>> print(type(basis))
<type 'tuple'>
>>> print([x(3) for x in basis])
[6, 6, 6]
>>> #LIST
... basis = list((lambda x: n*x) for n in [0, 1, 2])
>>> print(type(basis))
<type 'list'>
>>> print([x(3) for x in basis])
[6, 6, 6]
>>> #CORRECTED LIST
... basis = list((lambda x, n=n: n*x) for n in [0, 1, 2])
>>> print(type(basis))
<type 'list'>
>>> print([x(3) for x in basis])
[0, 3, 6]
相關的問題:http://stackoverflow.com/q/139819/4279 – jfs 2010-12-13 07:03:39