📜  Python – all()函数

📅  最后修改于: 2022-05-13 01:55:05.254000             🧑  作者: Mango

Python – all()函数

all()函数是Python中的一个内置函数,如果给定可迭代对象(列表、字典、元组、集合等)的所有元素都为真,则返回真,否则返回假。如果可迭代对象为空,它也会返回 True。

示例 #1:使用列表处理 all():

代码:

Python3
# All elements of list are true
l = [4, 5, 1]
print(all(l))
 
# All elements of list are false
l = [0, 0, False]
print(all(l))
 
# Some elements of list are
# true while others are false
l = [1, 0, 6, 7, False]
print(all(l))
 
# Empty List
l = []
print(all(l))


Python3
# All elements of tuple are true
t = (2, 4, 6)
print(all(t))
 
# All elements of tuple are false
t = (0, False, False)
print(all(t))
 
# Some elements of tuple
# are true while others are false
t = (5, 0, 3, 1, False)
print(all(t))
 
# Empty tuple
t = ()
print(all(t))


Python3
# All elements of set are true
s = {1, 1, 3}
print(all(s))
 
# All elements of set are false
s = {0, 0, False}
print(all(s))
 
# Some elements of set
# are true while others are false
s = {1, 2, 0, 8, False}
print(all(s))
 
# Empty set
s = {}
print(all(s))


Python3
# All elements of dictionary are true
d = {1: "Hello", 2: "Hi"}
print(all(d))
 
# All elements of dictionary are false
d = {0: "Hello", False: "Hi"}
print(all(d))
 
# Some elements of dictionary
# are true while others are false
d = {0: "Salut", 1: "Hello", 2: "Hi"}
print(all(d))
 
# Empty dictionary
d = {}
print(all(d))


Python3
# Non-Empty String
s = "Hi There!"
print(all(s))
 
# Non-Empty String
s = "000"
print(all(s))
 
# Empty string
s = ""
print(all(s))


输出:

True
False
False
True

示例 2:使用元组处理 all()。

蟒蛇3

# All elements of tuple are true
t = (2, 4, 6)
print(all(t))
 
# All elements of tuple are false
t = (0, False, False)
print(all(t))
 
# Some elements of tuple
# are true while others are false
t = (5, 0, 3, 1, False)
print(all(t))
 
# Empty tuple
t = ()
print(all(t))

输出:

True
False
False
True

示例 #3:使用集合处理 all()。

蟒蛇3

# All elements of set are true
s = {1, 1, 3}
print(all(s))
 
# All elements of set are false
s = {0, 0, False}
print(all(s))
 
# Some elements of set
# are true while others are false
s = {1, 2, 0, 8, False}
print(all(s))
 
# Empty set
s = {}
print(all(s))

输出:

True
False
False
True

示例 #4:使用字典处理 all()。

注意:对于字典,如果字典的所有键都为真或字典为空,则 all() 返回真,否则返回假。

蟒蛇3

# All elements of dictionary are true
d = {1: "Hello", 2: "Hi"}
print(all(d))
 
# All elements of dictionary are false
d = {0: "Hello", False: "Hi"}
print(all(d))
 
# Some elements of dictionary
# are true while others are false
d = {0: "Salut", 1: "Hello", 2: "Hi"}
print(all(d))
 
# Empty dictionary
d = {}
print(all(d))

输出:

True
False
False
True

示例 5:使用字符串处理 all()。

蟒蛇3

# Non-Empty String
s = "Hi There!"
print(all(s))
 
# Non-Empty String
s = "000"
print(all(s))
 
# Empty string
s = ""
print(all(s))

输出:

True
True
True