簡體   English   中英

python3如何匹配==對多個字符串

[英]python3 How to match == against multiple strings

我正在嘗試使用==來匹配多個結果。

這是我的問題:

if string1.lower().rstrip() == "remove from list" or "del from list":
    do something
else:
    do something else

我想完全匹配“從列表中刪除”,或傳遞給不同的函數,或者精確匹配“del from list”或傳遞給不同的函數。

添加到示例中,'傳遞給我提到的不同功能將是......

if string1.lower().rstrip() == "remove from list me" or "del from list me"

所以匹配exacty很重要,這就是我使用==而不是像'in'這樣的替代方法

我還希望能夠擴展此處的任何方法,以便能夠根據需要匹配4,5,6個字符串。

使用in和要匹配的字符串列表,如果字符串等於列表中的一個字符串,則為True

if string1.lower().rstrip() in ["remove from list", "del from list"]:

或者使用==兩次:

if string1.lower().rstrip() == "remove from list" or string1.lower().rstrip() == "del from list"]:

這, string1.lower().rstrip() == "remove from list" or "del from list" ,將始終評估為Trueelse將永遠不會到達,因為非空字符串的布爾值為True ,所以它等同於string1.lower().rstrip() == "remove from list" or True ,它始終為True

暫無
暫無

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

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