当我在列表和元组之间更改输出类型时,一个显示输出,而另一个显示结果的位置。为什么会这样?
>>> symbol = '123456789'
>>> (s for s in symbol)
<generator object <genexpr> at 0x7fc0e9e2fbf8>
>>> [s for s in symbol]
['1', '2', '3', '4', '5', '6', '7', '8', '9']
答案 0 :(得分:-1)
(s for s in symbol)
是生成器理解。类似于:
symbol = '123456789'
def func():
for s in symbol:
yield s
g = func()
print(g)
<generator object func at 0x7ffff7e59410>
print(tuple(g))
('1', '2', '3', '4', '5', '6', '7', '8', '9')
如果将生成器提供给list()
或tuple()
,则生成器将运行。