[Python]:何时使用'any(x)`函数?

[Python]:何时使用'any(x)`函数?,python,python-3.x,cpython,any,Python,Python 3.x,Cpython,Any,我正在阅读3.4版的Python手册,突然发现了一个我不知道的内置函数。该函数是any(x) Python手册说,如果iterable的任何元素为True,该函数“返回True。如果iterable为空,则返回False。” 他们还编写了与此函数等效的代码 def any(iterable): for element in iterable: if element: return True return False 此功能的用途是什么?例

我正在阅读3.4版的Python手册,突然发现了一个我不知道的内置函数。该函数是
any(x)

Python手册说,如果iterable的任何元素为True,该函数“返回
True
。如果iterable为空,则返回False。”

他们还编写了与此函数等效的代码

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

此功能的用途是什么?

例如,您可以使用它来避免多种类似情况

要检查字符串是否包含子字符串列表,可以执行以下操作:

str = 'Your cat is hungry.'

if 'cat' in str:
    print 'Feed it!'
elif 'dog' in str:
    print 'Feed it!'
elif 'hamster' in str:
    print 'Feed it!'
str = 'Your cat is hungry.'
pets = ['cat', 'dog', 'hamster']

if any(animal in str for animal in pets):
    print 'Feed it!'
…或者您可以:

str = 'Your cat is hungry.'

if 'cat' in str:
    print 'Feed it!'
elif 'dog' in str:
    print 'Feed it!'
elif 'hamster' in str:
    print 'Feed it!'
str = 'Your cat is hungry.'
pets = ['cat', 'dog', 'hamster']

if any(animal in str for animal in pets):
    print 'Feed it!'
更新:
if元素:返回True
。 您是正确的-如果iterable中的元素有值,则该值为
True
。在Python中,基本上,如果变量有一个值,它就是
True
——显然,只要该值不是
False
。运行该示例并查看值和条件,可能它不仅仅有助于解释:

x = ' '
y = ''
z = False

if x:
    print 'x is True!'
else:
    print 'x is False!'

if x == True:
    print 'x is True!'
else:
    print 'x is False!'

if y:
    print 'y is True!'
else:
    print 'y is False!'    

if z:
    print 'z is True!'
else:
    print 'z is False!'      
现在回到
any()
:它将任何iterable(如列表)作为参数-如果该iterable的任何值
True
(因此得名),
any()
返回
True

还有一个函数名为
all()
——它类似于
any()
,但仅当iterable的所有值都为True时才返回True:

print any([1, 2, False])

print all([1, 2, False])
真假 之前,@Burhan Khalid在评论中提到过这一点,但这里也应该提到关于被认为是
False
的官方文件:


例如,您可以使用它来避免多种类似情况

要检查字符串是否包含子字符串列表,可以执行以下操作:

str = 'Your cat is hungry.'

if 'cat' in str:
    print 'Feed it!'
elif 'dog' in str:
    print 'Feed it!'
elif 'hamster' in str:
    print 'Feed it!'
str = 'Your cat is hungry.'
pets = ['cat', 'dog', 'hamster']

if any(animal in str for animal in pets):
    print 'Feed it!'
…或者您可以:

str = 'Your cat is hungry.'

if 'cat' in str:
    print 'Feed it!'
elif 'dog' in str:
    print 'Feed it!'
elif 'hamster' in str:
    print 'Feed it!'
str = 'Your cat is hungry.'
pets = ['cat', 'dog', 'hamster']

if any(animal in str for animal in pets):
    print 'Feed it!'
更新:
if元素:返回True
。 您是正确的-如果iterable中的元素有值,则该值为
True
。在Python中,基本上,如果变量有一个值,它就是
True
——显然,只要该值不是
False
。运行该示例并查看值和条件,可能它不仅仅有助于解释:

x = ' '
y = ''
z = False

if x:
    print 'x is True!'
else:
    print 'x is False!'

if x == True:
    print 'x is True!'
else:
    print 'x is False!'

if y:
    print 'y is True!'
else:
    print 'y is False!'    

if z:
    print 'z is True!'
else:
    print 'z is False!'      
现在回到
any()
:它将任何iterable(如列表)作为参数-如果该iterable的任何值
True
(因此得名),
any()
返回
True

还有一个函数名为
all()
——它类似于
any()
,但仅当iterable的所有值都为True时才返回True:

print any([1, 2, False])

print all([1, 2, False])
真假 之前,@Burhan Khalid在评论中提到过这一点,但这里也应该提到关于被认为是
False
的官方文件:


如果你喜欢物理,python中的any函数就像电路中开关的并联。 如果任何一个开关接通,(
If element
)电路将完成,并使串联连接的灯泡发光。

让我们将图中所示的并联灯泡作为电路和 作为指示灯灯泡的灯泡(任何结果)

例如,如果您有一个逻辑列表,其中包含True和False

logical_list = [False, False, False, False]
logical_list_1 = [True, False, False, False]

any(logical_list)
False  ## because no circuit is on(True) all are off(False)

any(logical_list_1)
True  ## because one circuit is on(True) remaining three are off(False)
或者您可以将其视为与的连接,因此如果迭代器的任何一个值为False,则结果将为False

对于字符串的情况,场景是相同的,只是含义发生了变化

'' empty string            -> False
'python' non empty string  -> True
试试这个:

trial_list = ['','','','']
trial_list_1 = ['python','','','']

any(trial_list)
False ## logically trial_list is equivalent to [False(''), False(''), False(''), False('')]

any(trial_list_1)
True ## logically trial_list_1 is equivalent to [True('python'), False(''), False('') , False('')]
对于单个非空字符串,any(非空字符串)始终为True 对于单个空字符串的情况,any(空字符串始终为False

any('')
False

any('python')
True

我希望这会有所帮助,

如果你喜欢物理,python中的任意函数就像电路中开关的并行连接。 如果任何一个开关接通,(
If element
)电路将完成,并使串联连接的灯泡发光。

让我们将图中所示的并联灯泡作为电路和 作为指示灯灯泡的灯泡(任何结果)

例如,如果您有一个逻辑列表,其中包含True和False

logical_list = [False, False, False, False]
logical_list_1 = [True, False, False, False]

any(logical_list)
False  ## because no circuit is on(True) all are off(False)

any(logical_list_1)
True  ## because one circuit is on(True) remaining three are off(False)
或者您可以将其视为与的连接,因此如果迭代器的任何一个值为False,则结果将为False

对于字符串的情况,场景是相同的,只是含义发生了变化

'' empty string            -> False
'python' non empty string  -> True
试试这个:

trial_list = ['','','','']
trial_list_1 = ['python','','','']

any(trial_list)
False ## logically trial_list is equivalent to [False(''), False(''), False(''), False('')]

any(trial_list_1)
True ## logically trial_list_1 is equivalent to [True('python'), False(''), False('') , False('')]
对于单个非空字符串,any(非空字符串)始终为True 对于单个空字符串的情况,any(空字符串始终为False

any('')
False

any('python')
True

我希望这有帮助,

通常你会将它与某种条件一起使用,例如:
如果有的话(I%2==0表示某些数字列表中的I)
-看看是否有偶数……如果你理解Python,它也会很有用。通常你会将它与某种条件一起使用,例如:
如果有的话(I%2==0表示某些数字列表中的I)
-看看是否有偶数…如果您理解Python,这也会很有用。谢谢您的回答。我仍然对问题的第二部分感到困惑
if element:return True
。问题是这段代码的具体含义。它的表述方式似乎是,如果element有值,那么return True。谢谢你的回答。我仍然对问题的第二部分感到困惑
if元素:return True
。问题是这段代码是什么意思。它的表述方式似乎是if元素有一个值,然后return True。谢谢你的回答。我在你写作时就开始思考了“如果迭代器的任何一个值为False,结果将为False”。这对字符串意味着什么。我知道any(x)函数可以用于字符串,那么在处理字符串时实现True的标准是什么。谢谢你的回答。我从你编写时开始思考”如果迭代器的任何一个值为False,结果将为False”。这对字符串意味着什么。我知道any(x)函数可以用于字符串,那么在处理字符串时实现True的标准是什么。