首页 > 解决方案 > 在 if 条件下使用两个 any 函数时出错

问题描述

这是一个非常简单(可能很愚蠢)的问题。但我已经尝试了一个小时。这是一个无效的语法语句,当我对任何函数使用两个 if 条件时。这是我使用的语句。

if any(table in sentence for table in (alias_dict["tables"].strip('][').split(', ')))==False and if any(column in sentence for column in (alias_dict["columns"].strip('][').split(', ')))==True:
    print("Hii success")

但我收到错误:

如果有的话(句子中的表对于(alias_dict [“tables”].strip('] [').split(','))中的表)==False 并且如果有(句子中的列对于(alias_dict [”中的列) columns"].strip('][').split(', ')))==True: ^ SyntaxError: invalid syntax

错误 (^) 显示在“和”附近。我也尝试给出“&&”。我也尝试为整体any()提供paranthesis。但它似乎没有成功。我知道这可能是我的一个愚蠢的错误。仍然感谢任何帮助。

标签: pythonif-statementsyntax-errorany

解决方案


语法是if thing and otherthing,不是if thing and if otherthing

比较起来False也有点奇怪。试试这个。

if not any(
        table in sentence for table in alias_dict["tables"].strip('][').split(', ')
      ) and any(
        column in sentence for column in alias_dict["columns"].strip('][').split(', ')):
    print("Hii success")

推荐阅读