stdlib

all

官方文档说明

1
2
3
4
5
6
7
8
all(iterable)
Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

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

示例代码1:

1
2
3
4
5
6
7
8
9
print ('empty str:', all(''))
print ('list element is str:', all(['a', 'b', 'c', 'd']))
print ('list one of element is empty str:', all(['a', 'b', '', 'd']))
print ('list has 0 int element:', all([0, 1,2, 3]))
print ('tuple:', all(('a', 'b', 'c', 'd')))
print ('tuple one of element is empty str:', all(('a', '', 'c', 'd')))
print ('emtpy list:', all([]))
print ('emtpy tuple:', all(()))
print ('tuple, element is emtpy str', all(('', '', '', '')))

输出结果

1
2
3
4
5
6
7
8
9
empty str: True
list element is str: True
list one of element is empty str: False
list has 0 int element: False
tuple: True
tuple one of element is empty str: False
emtpy list: True
emtpy tuple: True
tuple, element is emtpy str False

any

官方文档说明

1
2
3
4
5
6
7
8
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

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

示例代码1

1
2
3
4
5
6
7
8
print ('str:' , any('123'))
print ('empty_str:', any(''))
print ("list:", any([1, 2, 3]))
print ('empty list:', any([]))
print ('list element is int:', any(['1', '2', '']))
print ('tuple:', any((1, 2, 3)))
print ('empty tuple:', any(()))
print ("tuple element is str", any(('1', '', '')))

输出结果

1
2
3
4
5
6
7
8
str: True
empty_str: False
list: True
empty list: False
list element is int: True
tuple: True
empty tuple: False
tuple element is str True