6.0 KiB
Python any()函数
原文:https://www.pythonforbeginners.com/basics/python-any-function
在 python 中,我们通常使用比较运算符和逻辑运算符来检查不同数量元素的条件。如果您必须检查元素列表中的条件,该怎么办?在本文中,我们将讨论 python 中的 any()函数。我们还将看到如何对不同的可迭代对象使用any()函数。
Python 中的 any()是什么?
any()函数用于检查在一个 iterable 对象中是否存在一个元素,该元素的值是否为 True。any()函数将列表、元组、集合、字典或字符串等可迭代对象作为其输入参数。执行后,如果 iterable 中至少有一个元素的值为True,它将返回True。否则返回False。您可以在下面的示例中观察到这一点。
myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more elements that evaluate to True:", output)
输出:
The list is: [1, 2, 3, 4]
List contains one or more elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more elements that evaluate to True: False
The list is: []
List contains one or more elements that evaluate to True: False
你可以把any()函数的工作理解为or操作符的一个应用。对于具有元素element1, element2, element3,.... elementN的可迭代对象,使用any()相当于执行语句 element1 OR element2 OR element3 OR ….., OR elementN。
Python 中带有可迭代对象的 any()函数
当我们将一个列表作为输入参数传递给 any()函数时,如果列表的元素中至少有一个元素的值为 True,它将返回 True。
myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more elements that evaluate to True:", output)
输出:
The list is: [1, 2, 3, 4]
List contains one or more elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more elements that evaluate to True: False
The list is: []
List contains one or more elements that evaluate to True: False
当我们将一个空列表传递给any() 函数时,它返回 False。
当我们将任何非空字符串作为输入参数传递给any()函数时,它返回True。
myStr1="PythonForBeginners"
myStr2=""
print("The string is:", myStr1)
output = any(myStr1)
print("The string contains one or more elements:", output)
print("The string is:", myStr2)
output = any(myStr2)
print("The string contains one or more elements:", output)
输出:
The string is: PythonForBeginners
The string contains one or more elements: True
The string is:
The string contains one or more elements: False
对于空字符串,any()函数返回False。
与列表类似,当我们将一个集合作为输入参数传递给 any()函数时,如果集合的元素中至少有一个元素的值为 True,则返回 True。
mySet1 = {1, 2, 3, 4}
mySet2 = {False}
mySet3 = set()
print("The Set is:", mySet1)
output = any(mySet1)
print("Set contains one or more elements that evaluate to True:", output)
print("The Set is:", mySet2)
output = any(mySet2)
print("Set contains one or more elements that evaluate to True:", output)
print("The Set is:", mySet3)
output = any(mySet3)
print("Set contains one or more elements that evaluate to True:", output)
输出:
The Set is: {1, 2, 3, 4}
Set contains one or more elements that evaluate to True: True
The Set is: {False}
Set contains one or more elements that evaluate to True: False
The Set is: set()
Set contains one or more elements that evaluate to True: False
当我们将一个空集传递给any()函数时,它返回False。
Python 中带字典的 any()函数
当我们将字典作为输入参数传递给any()函数时,如果 python 字典中至少有一个键的值为True,它将返回True。否则返回False。
myDict1 = {1: 1, 2: 2, 3: 3, 4: 4}
myDict2 = {False: 1}
myDict3 = {}
print("The Dictionary is:", myDict1)
output = any(myDict1)
print("Dictionary contains one or more keys that evaluate to True:", output)
print("The Dictionary is:", myDict2)
output = any(myDict2)
print("Dictionary contains one or more keys that evaluate to True:", output)
print("The Dictionary is:", myDict3)
output = any(myDict3)
print("Dictionary contains one or more keys that evaluate to True:", output)
输出:
The Dictionary is: {1: 1, 2: 2, 3: 3, 4: 4}
Dictionary contains one or more keys that evaluate to True: True
The Dictionary is: {False: 1}
Dictionary contains one or more keys that evaluate to True: False
The Dictionary is: {}
Dictionary contains one or more keys that evaluate to True: False
当我们将一个空字典传递给any()函数时,它返回False。
结论
在本文中,我们讨论了 python 中的 any()函数。我们还对不同的可迭代对象使用了any()函数,并观察了函数的输出。要了解更多关于 python 编程的知识,你可以在列表理解上阅读这篇文章。