如何在 if 条件下使用不等于运算符?

How to use not equal to operator in if condition?

在其他语言中,我们可以使用以下代码:

if(!False):
  print('not equal to operator worked.')

但是,如果我尝试在 python 中实施它,我会收到 'invalid syntax' 错误。有些人可能会说写 True 而不是 False,但在我的用例中这是不可能的。我的用例如下:

 def get_wrong_dtype_rows(col_name, data_type):
     arr = []
     for i,val in enumerate(df[col_name]):
        if !(type(val) is data_type):
            arr.append(i)
     return arr

我想知道,他们是解决这个问题的替代方法吗?

使用'not'。 'not False' 给出 'True' 等等。

在Python、

not 等于 !

所以,您的代码可能是

 def get_wrong_dtype_rows(col_name, data_type):
     arr = []
     for i,val in enumerate(df[col_name]):
        if not type(val) is data_type:
            arr.append(i)
     return arr