my_list1 = [30,34,56]
my_list2 = [29,500,43]
How to I check if all values in list are >= 30? my_list1
should work and my_list2
should not.
The only thing I could think of doing was:
boolean = 0
def func(ls):
for k in ls:
if k >= 30:
boolean = boolean + 1
else:
boolean = 0
if boolean > 0:
print 'Continue'
elif boolean = 0:
pass
Update 2016:
In hindsight, after dealing with bigger datasets where speed actually matters and utilizing numpy
...I would do this:
>>> my_list1 = [30,34,56]
>>> my_list2 = [29,500,43]
>>> import numpy as np
>>> A_1 = np.array(my_list1)
>>> A_2 = np.array(my_list2)
>>> A_1 >= 30
array([ True, True, True], dtype=bool)
>>> A_2 >= 30
array([False, True, True], dtype=bool)
>>> ((A_1 >= 30).sum() == A_1.size).astype(np.int)
1
>>> ((A_2 >= 30).sum() == A_2.size).astype(np.int)
0
You could also do something like:
len([*filter(lambda x: x >= 30, my_list1)]) > 0
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…