在Python中比较元组

发布于 2024-12-21 21:50:13 字数 441 浏览 2 评论 0原文

我有以下代码:

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

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

发布评论

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

评论(3

迷爱 2024-12-28 21:50:13

测试身份,而不是平等。你想要 current != Problem.getStartState()

有一个成语 is (not) None 可以使用,因为 None 保证是单例。除非您真的这么想,否则不要将其用于其他类型!

is tests for identity, not equality. You want current != problem.getStartState()

There is an idiom is (not) None which works because None is guaranteed to be a singleton. Don't use it for other types unless you really mean it!

许久 2024-12-28 21:50:13
while current != problem.getStartState():

    print "Current: ", current, "Start: ", problem.getStartState()

is 是身份(相同对象)比较器。在您的情况下,您需要一个相等(或不等式)(具有相同值的对象)运算符。

while current != problem.getStartState():

    print "Current: ", current, "Start: ", problem.getStartState()

is is the identity (same objects) comparator. In your case, you need an equality (or inequality) (objects with same values) operator.

一梦浮鱼 2024-12-28 21:50:13

is 不是在这种情况下使用的正确检查。
要比较 2 个元组,只需使用 != 或 ==

例如,问题可以解决如下:

while current != problem.getStartState():   
        print "Current: ", current, "Start: ", problem.getStartState()

干杯,

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:

while current != problem.getStartState():   
        print "Current: ", current, "Start: ", problem.getStartState()

cheers,

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