簡體   English   中英

if條件下如何使用不等於運算符?

[英]How to use not equal to operator in if condition?

在其他語言中,我們可以使用以下代碼:

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

但是,如果我嘗試在 python 中實現它,我會收到“無效語法”錯誤。 有些人可能會說寫 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

我想知道,他們是解決這個問題的替代方法嗎?

在 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

使用“不”。 'not False' 給出 'True' 等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM