python3如何将==与多个字符串匹配

时间:2019-03-16 16:09:02

标签: python python-3.x

我正在尝试使用- name: ensure that puppet lock file is created wait_for: path: /var/lib/puppet/state/agent_catalog_run.lock timeout: 1800 来匹配多个结果。

这是我的问题所在

==

我想完全匹配“从列表中删除”,或者传递给另一个函数,或者完全匹配“从列表中删除”,或者传递给另一个函数。

添加到示例中,“我提到的传递给其他功能的方法是...

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

因此精确匹配非常重要,这就是为什么即时通讯使用==代替“ in”之类的原因

我还希望能够扩展此处可用的任何方法,以便能够根据需要匹配4、5、6个字符串。

1 个答案:

答案 0 :(得分:3)

使用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"的总值将为True,将永远不会达到else,因为非空字符串的布尔值为True,所以它是等同于string1.lower().rstrip() == "remove from list" or True,始终为True