如果列表中的所有元素都等于某个值

25 浏览
0 Comments

如果列表中的所有元素都等于某个值

在使用Python 2.6的情况下,有没有一种方法可以在一句话中检查序列的所有项是否等于给定值?

[伪代码]
my_sequence = (2,5,7,82,35)
if 所有值都是int类型的(type(i) for i in my_sequence):
     执行操作()

而不是像这样:

my_sequence = (2,5,7,82,35)
all_int = True
for i in my_sequence:
    if type(i) is not int:
        all_int = False
        break
if all_int:
    执行操作()

0