gpt4 book ai didi

python - 使用 print 和 for 循环函数打印 1 到 99

转载 作者:太空狗 更新时间:2023-10-29 22:05:22 25 4
gpt4 key购买 nike

我在 Python 3.6 的 IDLE 中尝试了以下内容

print(value for value in range(1,100))

在 IDLE 中产生一条消息,上面写着

<generator object <genexpr> at 0x101b73a40>

我不明白这是什么意思。我做错了什么吗?

最佳答案

(value for value in range(1,100)) 生成generator对象,如果要打印list,只需将其包裹在[]

print([value for value in range(1,100)])

或者你可以简单地

print(list(range(1,100)))

您可以阅读什么是生成器 HERE

A function which returns a generator iterator. It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.

Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.

关于python - 使用 print 和 for 循环函数打印 1 到 99,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com