SyntaxError:'产量'在Python中的功能之外
我已经阅读了许多有关此帖子的文章,我意识到很多人都问了这个问题,但是我不明白我在做错什么。我正在尝试为我正在从事的项目绘制一个带有坐标的网格,并且我正在尝试一些代码来生成它。我尝试了这个,但是我一直遇到这个错误。代码怎么了?我尝试了不同的凹痕,但行不通。
stepsize = 0.001
for x in range(0, 10, stepsize):
for y in range(0, 10, stepsize):
yield (x,y)
^^原始Q+
感谢所有帮助的人,我不确定为什么要发布该代码,因为它无论如何都无法正常工作!对不起,我的误解,我以前从未使用过发电机。 :)
I've read numerous posts on this, and I realise lots of people have asked this, but I can't understand what I'm doing wrong. I'm trying to draw a grid with coordinates for a project I'm working on, and I was trying some code for generating it. I tryed this one, but I keep getting that error. What's wrong with the code? I've tryed different indentations but it doesn't work.
stepsize = 0.001
for x in range(0, 10, stepsize):
for y in range(0, 10, stepsize):
yield (x,y)
How can I generate a regular geographic grid using python?
^^The original q+a
Thanks to everyone who helped, I'm not sure why that code was posted considering it wouldn't work anyway! Sorry for my misunderstanding, I haven't used generators before. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
收益
关键字用于从函数堆栈溢出问题在收益率关键字上
在函数之外不能像您的代码一样使用它。
这是在
def
语句中如果 但是这些行行不通:
因为
stepize
是一个浮点,range
函数仅处理整数 - 它会说typeerror:'float'对象不能解释为整数
The
yield
keyword is used to "return" a generator object from a functionStack Overflow Question on the yield keyword
It cannot be used outside a function, like in your code.
If this was under a
def
statement, then it would be fine, however there is nodef
statement here, so it raises an errorAlso, not in relation to the question, but these lines won't work:
because
stepsize
is a float, and therange
function only handles integers - it will say something likeTypeError: 'float' object cannot be interpreted as an integer
为了添加已经说的内容,您可以通过编写生成器来生成所需的输出来实现代码。
示例:
您还可以使用
product
来产生双重增量:结合您可以做的更多Itertools:
To add to what was already said, you could implement your code by composing generators to produce the required output.
Example:
You could also use
product
to produce the double increment:Combining more itertools you could do: