有没有办法把这些 if 写得更好一些?
我需要用 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 if
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如有疑问,请应用数学。 ;)
When in doubt, apply maths. ;)
马格努斯的建议无疑是对您提出的问题的正确答案,但一般来说,您想使用字典来解决这样的问题:
即使在这种情况下,我可以争论使用字典更好,因为很明显有四种状态并且它们重复,但是很难抗拒所有数学的纯粹之美。
顺便说一句,你问题中的代码有其中有一个错误,并且,假设您测试的值是唯一可能的值,相当于:
错误是您需要
elif
作为第二个及后续条件,否则您将继续测试dx
和dy
更改后。如果它们是1
和0
,那么您的所有条件都将成立,并且最终结果相同!如果它们以0
和1
开头,那么第二个条件和所有后续条件都将为 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:
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:
The bug is that you need
elif
for the second and subsequent conditions, otherwise you're continuing to testdx
anddy
after changing them. If they're1
and0
, then all your conditions will be true and they end up the same at the end! If they start out as0
and1
then the second and all subsequent conditions will be true, and you again end up with1, 0
. And so on...只是扩展马格努斯的答案。如果您将 [dx, dy] 想象为一个向量,那么您实际上所做的是 旋转 90 度(或PI/2)。
要计算此值,您可以使用以下转换:
在您的情况下转换为:
由于
sin (90) = 1
和cos(90) = 0
我们将其简化为:就这样!
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:
Which in your case translate to:
Since
sin(90) = 1
andcos(90) = 0
we simplify it to:And there you have it!
您正在使用的值似乎是一个连续旋转的单位向量 - 换句话说,一个相量。 复数是坐标,因此:
假设我对 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:
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.
虽然我会同意 Magnus 的答案,但这里还有另一种旋转一组值的方法:
请注意,在
for dx, dy
循环中的某处应该有一个break
否则它永远不会结束。While I would go with Magnus' answer, here's yet another approach for rotating over a set of values:
Note that there should be a
break
somewhere in thefor dx, dy
loop or else it will never end.