3 回答

慕容森
TA贡献1853条经验 获得超18个赞
您将返回True正确的位置,但是如果第一项不匹配,该函数将False立即返回,而不是继续循环。只需将移到return False函数的末尾,即循环之外:
def checks(a,b):
for item in a:
if b[1] == item[1]:
return True
return False
True如果项目匹配False则返回,如果循环不匹配则返回。
无论如何,这解释了为什么您的代码无法正常工作,而是any按照其他人的建议使用Pythonic的原因。=)

宝慕林4294392
TA贡献2021条经验 获得超8个赞
您可以any()在这里使用:
def checks(a,b):
return any (b[1] == item[1] for item in a)
>>> checks(['5v','7y'],'6y')
True
>>> checks(['5v','7z'],'6y')
False
帮助any:
>>> print any.__doc__
any(iterable) -> bool
Return True if bool(x) is True for any x in the iterable.
If the iterable is empty, return False.
添加回答
举报
0/150
提交
取消