测试字典键是否存在的条件始终为False

编程入门 行业动态 更新时间:2024-10-19 04:22:12
本文介绍了测试字典键是否存在的条件始终为False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我制作了一个使用两个字典curr_stats和weekly_result的函数.如果weekly_result中有任何不在curr_stats中的键,则该函数应该只打印invalid_msg,而没有curr_stats的突变.

I made a function that consumes two dictionaries, curr_stats and weekly_result. If there are any keys in weekly_result that aren't in curr_stats, the function is supposed to just print invalid_msg, with no mutation of curr_stats.

但是我代码第5行上的if语句似乎不起作用.应该触发下一个if语句,所以不会发生curr_stats的变异.

But the if statement on the 5th line of my code doesn't seem to be working. It's supposed to trigger the next if statement, so no mutation of curr_stats occurs.

def update_standings(curr_stats, weekly_result): invalid = 0 point_counter(weekly_result) for team in weekly_result: if team in curr_stats == False: invalid = invalid + 1 if invalid > 0: print(invalid_msg) else: for team in weekly_result: curr_stats[team] = curr_stats[team] + weekly_result[team]

推荐答案

在Python中,所有比较都具有相同优先级,包括in. 发生了什么事比较链,一种旨在测试传递关系的特殊形式就像在数学课上一样:

In Python, all comparisons have the same precedence, including in. What's happening is comparison chaining, a special form intended to test transitive relationships like in math class:

if x_min < x < x_max: ...

如Paweł Kordowski在他的评论中指出,上述比较链主要等同于:

As Paweł Kordowski pointed out in his comment, the above comparison chain is mostly equivalent to:

if x_min < x and x < x_max: ...

(有一个区别: 等效"代码可能会对x进行两次评估,而比较链则对x进行一次评估.)

(There is one difference: The "equivalent" code might evaluate x twice, while the comparison chain evaluates x exactly once.)

在您的情况下,比较链为:

In your case, the comparison chain is:

if team in curr_stats == False: ...

...(主要)等同于:

...which is (mostly) equivalent to:

if team in curr_stats and curr_stats == False: ...

仅当curr_stats包含team 并且 curr_stats为空...这是绝对不会发生的.

This is only true if curr_stats contains team and curr_stats is empty... which should never happen.

您的代码的问题是== False ---的部分原因是它将比较变成了比较链,但是主要是因为您根本不需要它. 当您想要布尔值的反义词时,Python提供not关键字. 您的条件语句应为:

The problem with your code is the == False --- in part because it turned a comparison into a comparison chain, but mostly because you never needed it in the first place. Python provides the not keyword for when you want a Boolean's opposite. Your conditional statement should read:

if team not in curr_stats: invalid = invalid + 1

最后一条建议: 通过删除invalid计数器,并在找到无效的team后立即返回,可以使该功能更短. (一旦您发现weekly_result是无效的输入,您可能就不在乎它是"更多无效"了.) 我还使用了 dict.items 来简化最终的循环:

One last suggestion: This function can be made even shorter by getting rid of the invalid counter and just returning as soon as an invalid team is found. (Once the you've discovered that weekly_result is invalid input, you probably don't care if it's "even more invalid".) I also used dict.items to simplify the final for loop:

def update_standings(curr_stats, weekly_result): point_counter(weekly_result) for team in weekly_result: if team not in curr_stats: print(invalid_msg) return for team, result in weekly_result.items(): curr_stats[team] += result

更多推荐

测试字典键是否存在的条件始终为False

本文发布于:2023-11-06 14:08:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   字典   条件   测试   False

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!