有没有办法把这些 if 写得更好一些?

发布于 2025-01-01 12:58:35 字数 340 浏览 0 评论 0原文

我需要用 Python 编写这四个 if。请注意它的作用,是在循环中的四种可能状态之间进行更改:1,0 -> 0,1-> -1,0-> 0,-1 并返回到第一个。

if [dx, dy] == [1,0]:
    dx, dy = 0, 1
if [dx, dy] == 0, 1:
    dx, dy = -1, 0
if [dx, dy] == [-1, 0]
    dx, dy = 0, -1
if [dx, dy] == [0, -1]:
    dx, dy = 1, 0

谁能建议我更好/更好的方式来写这个?

I need to write these four ifs in Python. Notice what it does, is changing between four possible states in a loop: 1,0 -> 0,1 -> -1,0 -> 0,-1 and back to first.

if [dx, dy] == [1,0]:
    dx, dy = 0, 1
if [dx, dy] == 0, 1:
    dx, dy = -1, 0
if [dx, dy] == [-1, 0]
    dx, dy = 0, -1
if [dx, dy] == [0, -1]:
    dx, dy = 1, 0

Can anyone suggest me a better/nicer way to write this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

2025-01-08 12:58:35
dx, dy = -dy, dx

如有疑问,请应用数学。 ;)

dx, dy = -dy, dx

When in doubt, apply maths. ;)

怀念你的温柔 2025-01-08 12:58:35

马格努斯的建议无疑是对您提出的问题的正确答案,但一般来说,您想使用字典来解决这样的问题:

statemap = {(1, 0): (0, 1), (0, 1): (-1, 0), (-1, 0): (0, -1), (0, -1): (1, 0)}

dx, dy = statemap[dx, dy]

即使在这种情况下,我可以争论使用字典更好,因为很明显有四种状态并且它们重复,但是很难抗拒所有数学的纯粹之美。

顺便说一句,你问题中的代码有其中有一个错误,并且,假设您测试的值是唯一可能的值,相当于:

dx, dy = 1, 0

错误是您需要 elif 作为第二个及后续条件,否则您将继续测试 dxdy 更改后。如果它们是 10,那么您的所有条件都将成立,并且最终结果相同!如果它们以 01 开头,那么第二个条件和所有后续条件都将为 true,并且您再次以 1, 0 结束。等等...

Magnus' suggestion is undeniably the right answer to your question as posed, but generally speaking, you want to use a dictionary for problems like this:

statemap = {(1, 0): (0, 1), (0, 1): (-1, 0), (-1, 0): (0, -1), (0, -1): (1, 0)}

dx, dy = statemap[dx, dy]

Even in this case I could argue using a dictionary is better, since it's clear that there are exactly four states and that they repeat, but it's hard to resist the sheer beauty of all teh maths.

By the way, the code in your question has a bug in it, and, assuming that the values you test for are the only possible values, is equivalent to:

dx, dy = 1, 0

The bug is that you need elif for the second and subsequent conditions, otherwise you're continuing to test dx and dy after changing them. If they're 1 and 0, then all your conditions will be true and they end up the same at the end! If they start out as 0 and 1 then the second and all subsequent conditions will be true, and you again end up with 1, 0. And so on...

陈甜 2025-01-08 12:58:35

只是扩展马格努斯的答案。如果您将 [dx, dy] 想象为一个向量,那么您实际上所做的是 旋转 90 度(或PI/2)。

要计算此值,您可以使用以下转换:

二维旋转

在您的情况下转换为:

x = x * cos(90) - y * sin(90)
y = x * sin(90) + y * cos(90)

由于 sin (90) = 1cos(90) = 0 我们将其简化为:

x, y = -y, x

就这样!

Just extending Magnus answer. If you imagine [dx, dy] as a vector, what you're actually doing is a rotation of 90 degrees (or PI/2).

To calculate this, you can use the following transformation:

two dimensional rotation

Which in your case translate to:

x = x * cos(90) - y * sin(90)
y = x * sin(90) + y * cos(90)

Since sin(90) = 1 and cos(90) = 0 we simplify it to:

x, y = -y, x

And there you have it!

趁微风不噪 2025-01-08 12:58:35

您正在使用的值似乎是一个连续旋转的单位向量 - 换句话说,一个相量。 复数是坐标,因此:

# at initialization
phase = 1
# at the point of modification
phase *= 1j
dx, dy = phase.real, phase.imag

假设我对 dx,dy 值含义的解释是正确的,这为您提供了额外的灵活性,以防以后您想在每个步骤中旋转一些其他量。

The values you're working with appear to be a unit vector that continuously rotates - in other words, a phasor. Complex numbers are coordinates, so:

# at initialization
phase = 1
# at the point of modification
phase *= 1j
dx, dy = phase.real, phase.imag

Assuming my interpretation of the meaning of the dx,dy values is correct, this gives you extra flexibility in case it turns out later that you want to rotate by some other amount in each step.

迷爱 2025-01-08 12:58:35

虽然我会同意 Magnus 的答案,但这里还有另一种旋转一组值的方法:

def rotate(*states):
    while 1:
        for state in states:
            yield state

for dx, dy in rotate((1, 0), (0, 1), (-1, 0), (0, -1)):
    pass

请注意,在 for dx, dy 循环中的某处应该有一个 break否则它永远不会结束。

While I would go with Magnus' answer, here's yet another approach for rotating over a set of values:

def rotate(*states):
    while 1:
        for state in states:
            yield state

for dx, dy in rotate((1, 0), (0, 1), (-1, 0), (0, -1)):
    pass

Note that there should be a break somewhere in the for dx, dy loop or else it will never end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文