我们都知道,有列表理解,比如
[i for i in [1, 2, 3, 4]]
还有字典理解,比如
{i:j for i, j in {1: 'a', 2: 'b'}.items()}
但是
(i for i in (1, 2, 3))
最终会变成一个发电机,而不是一个.为什么?
My guess is that a tuple
is immutable, 但是 this does not seem to be the answer.
我们都知道,有列表理解,比如
[i for i in [1, 2, 3, 4]]
还有字典理解,比如
{i:j for i, j in {1: 'a', 2: 'b'}.items()}
但是
(i for i in (1, 2, 3))
最终会变成一个发电机,而不是一个.为什么?
My guess is that a tuple
is immutable, 但是 this does not seem to be the answer.
您可以使用生成器表达式:
tuple(i for i in (1, 2, 3))
但是…的括号已经用过了生成器表达式.