any()函数用于检查可迭代对象的任何元素是否为True。它接受一个可迭代的对象并返回True,如果一个或多个元素为true,则返回False。
如果可迭代对象中的任何项目(例如列表,元组和字典)为true,则 any()函数返回True,否则返回False。
如果可迭代对象为空(例如list = [],tuple =()),则 any()函数返回False。
语法:
any(iterable_object)
参数: iterable_object –可迭代对象,如列表,元组,字典。
返回值: bool-布尔值
1)描述程序 any() 在Python中使用列表功能
if __name__ == "__main__": # any() function will return True because # 第一个项目在list_1中为true list_1 = [1, 2, 0, 3] result = any(list_1) print ("list_1: {}".format(result)) # any() function will return False because # list_2中没有一项是正确的 list_2 = [0, False, '', 0.0, [], {}, None] result = any(list_2) print ("list_2: {}".format(result)) # any() function will return False because # list_3为空 list_3 = [] result = any(list_3) print ("list_3: {}".format(result))
输出结果
list_1: True list_2: False list_3: False
2)描述程序 any() 在Python中使用字符串功能
if __name__ == "__main__": # any() function will return True because # 字符串_1不是无 string_1 = "Hello! python" print ("string_1: {}".format(any(string_1))) # any() function will return True because #string_2不是没有。此处,“ 0”为真,0为假 string_2 = "0" print ("string_2: {}".format(any(string_2))) # any() function will return False because # string_3为none string_3 = "" print ("string_3: {}".format(any(string_3)))
输出结果
string_1: True string_2: True string_3: False
3)描述程序 any() 在python中使用字典功能
如果是字典,如果所有键均为假, any()返回False。如果至少有一个键为真,any() 返回True。
if __name__ == "__main__": # any() function will return False because # dictonary_1中的key为False。 dictonary_1 = {0: 'False'} print("dictonary_1: {}".format(any(dictonary_1))) # any() function will return True because # dictonary_2中的第二个键(即1)为True。 dictonary_2 = {0: 'False', 1: 'True'} print("dictonary_2: {}".format(any(dictonary_2))) # any() function will return False because # dictonary_3中的所有键均为False。 dictonary_3 = {0: 'False', False: 0} print("dictonary_3: {}".format(any(dictonary_3))) # any() function will return False because # dictonary_4为空。 dictonary_4 = {} print("dictonary_4: {}".format(any(dictonary_4))) # any() function will return True because # dictonary_5中的键(即“ 0”)为True。 # 0为假 # '0'为真 dictonary_5 = {'0': 'False'} print("dictonary_5: {}".format(any(dictonary_5)))
输出结果
dictonary_1: False dictonary_2: True dictonary_3: False dictonary_4: False dictonary_5: True
4)描述程序 any() 在Python中使用元组功能
if __name__ == "__main__": # any() function will return True because # 元组_1中的第一项为True。 tuple_1 = (1, "BeginnersBook", 22) print("tuple_1: {}".format(any(tuple_1))) # any() function will return False because # tuple_2中的所有项目均为False。 tuple_2 = (0, False, 0) print("tuple_2: {}".format(any(tuple_2))) # any() function will return True because # tuple_3中的第一项为True。 tuple_3 = (1, 0, False) print("tuple_3: {}".format(any(tuple_3))) # any() function will return True because # tuple_4中的第二项为True。 tuple_4 = (False, 1, 2, 3) print("tuple_4: {}".format(any(tuple_4))) # any() function will return False because # tuple_5为空。 tuple_5 = () print("tuple_5: {}".format(any(tuple_5)))
输出结果
tuple_1: True tuple_2: False tuple_3: True tuple_4: True tuple_5: False