SyntaxError:'产量'在Python中的功能之外

发布于 2025-02-08 11:37:13 字数 592 浏览 0 评论 0原文

我已经阅读了许多有关此帖子的文章,我意识到很多人都问了这个问题,但是我不明白我在做错什么。我正在尝试为我正在从事的项目绘制一个带有坐标的网格,并且我正在尝试一些代码来生成它。我尝试了这个,但是我一直遇到这个错误。代码怎么了?我尝试了不同的凹痕,但行不通。

stepsize = 0.001

for x in range(0, 10, stepsize):
  for y in range(0, 10, stepsize):
    yield (x,y)

我如何使用python?<<<<<<<<<<< /a>

^^原始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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2025-02-15 11:37:13

收益关键字用于从函数

堆栈溢出问题在收益率关键字上

在函数之外不能像您的代码一样使用它。

这是在def语句中


如果 但是这些行行不通:

for x in range(0, 10, stepsize):
  for y in range(0, 10, stepsize):

因为stepize是一个浮点,range函数仅处理整数 - 它会说typeerror:'float'对象不能解释为整数

The yield keyword is used to "return" a generator object from a function

Stack 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 no def statement here, so it raises an error


Also, not in relation to the question, but these lines won't work:

for x in range(0, 10, stepsize):
  for y in range(0, 10, stepsize):

because stepsize is a float, and the range function only handles integers - it will say something like TypeError: 'float' object cannot be interpreted as an integer

似狗非友 2025-02-15 11:37:13

为了添加已经说的内容,您可以通过编写生成器来生成所需的输出来实现代码。

def increment(start, end, delta):
    while start < end:
        yield start
        start += delta

def double_increment(start, end, delta):
    for x in increment(start, end, delta):
        for y in increment(start, end, delta):
            yield x, y

示例:

for x, y in double_increment(0, 1, 0.4):
    print(f"{x:.2f} {y:.2f}")
0.00 0.00
0.00 0.40
0.00 0.80
0.40 0.00
0.40 0.40
0.40 0.80
0.80 0.00
0.80 0.40
0.80 0.80

您还可以使用product来产生双重增量:

from itertools import product
for x, y in product(increment(0, 1, 0.4), repeat=2):
    print(f"{x:.2f} {y:.2f}")

结合您可以做的更多Itertools:

from itertools import product, count, takewhile
for x, y in product(takewhile(lambda x: x < 1, count(0, step=0.4)), repeat=2):
    print(f"{x:.2f} {y:.2f}")

To add to what was already said, you could implement your code by composing generators to produce the required output.

def increment(start, end, delta):
    while start < end:
        yield start
        start += delta

def double_increment(start, end, delta):
    for x in increment(start, end, delta):
        for y in increment(start, end, delta):
            yield x, y

Example:

for x, y in double_increment(0, 1, 0.4):
    print(f"{x:.2f} {y:.2f}")
0.00 0.00
0.00 0.40
0.00 0.80
0.40 0.00
0.40 0.40
0.40 0.80
0.80 0.00
0.80 0.40
0.80 0.80

You could also use product to produce the double increment:

from itertools import product
for x, y in product(increment(0, 1, 0.4), repeat=2):
    print(f"{x:.2f} {y:.2f}")

Combining more itertools you could do:

from itertools import product, count, takewhile
for x, y in product(takewhile(lambda x: x < 1, count(0, step=0.4)), repeat=2):
    print(f"{x:.2f} {y:.2f}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文