在Python中比较元组
我有以下代码:
while current is not problem.getStartState():
print "Current: ", current, "Start: ", problem.getStartState()
现在由于某种原因比较效果不佳,您可以在以下输出中看到:
Current: (3, 5, 0, 0, 0, 0) Start: (4, 5, 0, 0, 0, 0)
Current: (4, 5, 0, 0, 0, 0) Start: (4, 5, 0, 0, 0, 0)
您可以看到即使 current 与 getStartState() 相同,它也会进入 while。此外,当它曾经是一个 2 字段元组 (x,y) 时,它工作得很好。
我做错了什么?谢谢
I have the following piece of code:
while current is not problem.getStartState():
print "Current: ", current, "Start: ", problem.getStartState()
now for some reason the comparison is not working well, you can see in the following output:
Current: (3, 5, 0, 0, 0, 0) Start: (4, 5, 0, 0, 0, 0)
Current: (4, 5, 0, 0, 0, 0) Start: (4, 5, 0, 0, 0, 0)
you can see that even though current is the same as getStartState() it enters the while. furthermore - when it used to be a 2 fields tuple (x,y) it worked fine.
What am I doing wrong ? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是
测试身份,而不是平等。你想要current != Problem.getStartState()
有一个成语
is (not) None
可以使用,因为None
保证是单例。除非您真的这么想,否则不要将其用于其他类型!is
tests for identity, not equality. You wantcurrent != problem.getStartState()
There is an idiom
is (not) None
which works becauseNone
is guaranteed to be a singleton. Don't use it for other types unless you really mean it!is
是身份(相同对象)比较器。在您的情况下,您需要一个相等(或不等式)(具有相同值的对象)运算符。is
is the identity (same objects) comparator. In your case, you need an equality (or inequality) (objects with same values) operator.is 不是在这种情况下使用的正确检查。
要比较 2 个元组,只需使用 != 或 ==
例如,问题可以解决如下:
干杯,
is is not the correct check to be used in this case.
To compare 2 tuples just use != or ==
for instance the problem can be solved as follows:
cheers,