python函数示例_Python中的any()函数与示例

python函数示例

Python any()函数 (Python any() function)

any() function is used to check whether any of the elements of an iterable object is True or not. It accepts an iterable object and returns True, if one or more than one elements are true, else it returns False.

any()函数用于检查可迭代对象的任何元素是否为True。 它接受一个可迭代的对象并返回True ,如果一个或多个元素为true,则返回False 。

If any item in iterable object (eg. list, tuple and dictionary) is true then any() function returns True otherwise False.

如果可迭代对象中的任何项(例如,列表,元组和字典)为true,则any()函数将返回True,否则返回False。

If iterable object is empty ( eg. list = [], tuple=() ) then any() function returns False.

如果可迭代对象为空(例如list = [] , tuple =() ),则any()函数将返回False。

Syntax:

句法:

    any(iterable_object)

Parameter: iterable_object – an iterable object like list, tuple, dictionary.

参数: iterable_object –可迭代对象,如列表,元组,字典。

Return value: bool – a Boolean value

返回值: bool-布尔值

1) Program to describe any() function with list in python

1)程序以python中的列表描述any()函数

if __name__ == "__main__":
    # any() function will return True because 
    # 1st item is true in list_1
    list_1 = [1, 2, 0, 3]
    result = any(list_1)
    print ("list_1: {}".format(result))
    
    # any() function will return False because 
    # none of the items are true in 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 is empty
    list_3 = []
    result = any(list_3)
    print ("list_3: {}".format(result))

Output

输出量

list_1: True
list_2: False
list_3: False

2) Program to describe any() function with string in python

2)用python中的字符串描述any()函数的程序

if __name__ == "__main__":
    # any() function will return True because 
    # string_1 is not none
    string_1 = "Hello! python"
    print ("string_1: {}".format(any(string_1)))
    
    # any() function will return True because 
    # string_2 is not none. Here, '0' is True and 0 is false
    string_2 = "0"
    print ("string_2: {}".format(any(string_2)))
    
    # any() function will return False because 
    # string_3 is none
    string_3 = ""
    print ("string_3: {}".format(any(string_3)))

Output

输出量

string_1: True
string_2: True
string_3: False

3) Program to describe any() function with dictionaries in python

3)用python中的字典来描述any()函数的程序

In case of dictionaries, If all keys are false, any() returns False. If atleast one key is true, any() returns True.

对于字典,如果所有键均为假,则any()返回False。 如果至少一个键为true,则any()返回True。

if __name__ == "__main__":
    # any() function will return False because 
    # key in dictonary_1 is False.
    dictonary_1 = {0: 'False'}
    print("dictonary_1: {}".format(any(dictonary_1)))
    
    # any() function will return True because 
    # second key(i.e 1) in dictonary_2 is True.
    dictonary_2 = {0: 'False', 1: 'True'}
    print("dictonary_2: {}".format(any(dictonary_2)))
    
    # any() function will return False because 
    # all keys in dictonary_3 is False.
    dictonary_3 = {0: 'False', False: 0}
    print("dictonary_3: {}".format(any(dictonary_3)))
    
    # any() function will return False because 
    # dictonary_4 is empty.
    dictonary_4 = {}
    print("dictonary_4: {}".format(any(dictonary_4)))
    
    # any() function will return True because 
    # key(i.e '0') in dictonary_5 is True.
    # 0 is False
    # '0' is True
    dictonary_5 = {'0': 'False'}
    print("dictonary_5: {}".format(any(dictonary_5)))

Output

输出量

dictonary_1: False
dictonary_2: True
dictonary_3: False
dictonary_4: False
dictonary_5: True

4) Program to describe any() function with tuple in python

4)在python中用元组描述any()函数的程序

if __name__ == "__main__":
    # any() function will return True because 
    # 1st item in tuple_1 is True.
    tuple_1 = (1, "BeginnersBook", 22)
    print("tuple_1: {}".format(any(tuple_1)))
    
    # any() function will return False because 
    # all items in tuple_2 is False.
    tuple_2 = (0, False, 0)
    print("tuple_2: {}".format(any(tuple_2)))
    
    # any() function will return True because 
    # 1st item in tuple_3 is True.
    tuple_3 = (1, 0, False)
    print("tuple_3: {}".format(any(tuple_3)))
    
    # any() function will return True because 
    # 2nd item in tuple_4 is True.
    tuple_4 = (False, 1, 2, 3)
    print("tuple_4: {}".format(any(tuple_4)))
    
    # any() function will return False because 
    # tuple_5 is empty.
    tuple_5 = ()
    print("tuple_5: {}".format(any(tuple_5)))   

Output

输出量

tuple_1: True
tuple_2: False
tuple_3: True
tuple_4: True
tuple_5: False


翻译自: https://www.includehelp.com/python/any-function-with-example.aspx

python函数示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值