I just started to teach myself Python, and I wanted to search an large array of strings for a couple of keywords. I tried using nested if statements but it's clunky and doesn't work.
我刚刚开始自学Python,我想搜索一大堆字符串中的几个关键字。我尝试使用嵌套的if语句,但它很笨重但不起作用。
Is there a easier way to do this? By the way, my array of strings is called tmp
.
有更简单的方法吗?顺便说一句,我的字符串数组称为tmp。
for i in range(0, len(idList)):
for j in range(0, len(tmp)):
if "apples" in tmp[j]:
if "bananas" in tmp[j]:
if "peaches" in tmp[j]:
if "pears" in tmp[j]:
*Do something*
2 个解决方案
#1
Your code is equivalent to:
您的代码相当于:
for i in range(0, len(idList)):
for j in range(0, len(tmp)):
if all(s in tmp[j] for s in ("apples", "bananas", "peaches", "pears")):
*Do something*
which makes it a bit shorter. The all()
function allows you to check multiple conditions and this function evaluates to true when all conditions are true.
这让它变得更短。 all()函数允许您检查多个条件,当所有条件都为真时,此函数的计算结果为true。
#2
From what you wrote it sounds like you're trying to find any of the keywords in the tmp
list, not every in one element. If that's the case, then your code should instead be
从你所写的内容来看,听起来你正试图找到tmp列表中的任何关键字,而不是每个元素中都有。如果是这种情况,那么您的代码应该是
for i in range(0, len(idList)):
for j in range(0, len(tmp)):
if "apples" in tmp[j]:
*Do something*
elif "bananas" in tmp[j]:
*Do something*
elif "peaches" in tmp[j]:
*Do something*
elif "pears" in tmp[j]:
*Do something*
so you would do something (different or the same) in each of those conditions. If "apples" is in the current element in the list, then do something. Or, if "bananas" is in the element, then do something else.
所以你会在每个条件中做一些事情(不同或相同)。如果“apples”位于列表中的当前元素中,则执行某些操作。或者,如果“香蕉”在元素中,那么做其他事情。
To shorten your code, take a look at any
:
要缩短代码,请查看以下内容:
Return
True
if any element of the iterable is true. If the iterable is empty, returnFalse
. Equivalent to:如果iterable的任何元素为true,则返回True。如果iterable为空,则返回False。相当于:
def any(iterable): for element in iterable: if element: return True return False
This would make your code similar to Simeon's example, but replacing all
for any
.
这将使您的代码类似于Simeon的示例,但替换所有代码。
#1
Your code is equivalent to:
您的代码相当于:
for i in range(0, len(idList)):
for j in range(0, len(tmp)):
if all(s in tmp[j] for s in ("apples", "bananas", "peaches", "pears")):
*Do something*
which makes it a bit shorter. The all()
function allows you to check multiple conditions and this function evaluates to true when all conditions are true.
这让它变得更短。 all()函数允许您检查多个条件,当所有条件都为真时,此函数的计算结果为true。
#2
From what you wrote it sounds like you're trying to find any of the keywords in the tmp
list, not every in one element. If that's the case, then your code should instead be
从你所写的内容来看,听起来你正试图找到tmp列表中的任何关键字,而不是每个元素中都有。如果是这种情况,那么您的代码应该是
for i in range(0, len(idList)):
for j in range(0, len(tmp)):
if "apples" in tmp[j]:
*Do something*
elif "bananas" in tmp[j]:
*Do something*
elif "peaches" in tmp[j]:
*Do something*
elif "pears" in tmp[j]:
*Do something*
so you would do something (different or the same) in each of those conditions. If "apples" is in the current element in the list, then do something. Or, if "bananas" is in the element, then do something else.
所以你会在每个条件中做一些事情(不同或相同)。如果“apples”位于列表中的当前元素中,则执行某些操作。或者,如果“香蕉”在元素中,那么做其他事情。
To shorten your code, take a look at any
:
要缩短代码,请查看以下内容:
Return
True
if any element of the iterable is true. If the iterable is empty, returnFalse
. Equivalent to:如果iterable的任何元素为true,则返回True。如果iterable为空,则返回False。相当于:
def any(iterable): for element in iterable: if element: return True return False
This would make your code similar to Simeon's example, but replacing all
for any
.
这将使您的代码类似于Simeon的示例,但替换所有代码。