Check if all values in list are greater than a certain number

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
324000 次浏览

Use the all()功能 with a generator expression:

>>> my_list1 = [30, 34, 56]
>>> my_list2 = [29, 500, 43]
>>> all(i >= 30 for i in my_list1)
True
>>> all(i >= 30 for i in my_list2)
False

注意,这个测试大于 或等于30,否则 my_list1也不会通过测试。

If you wanted to do this in a function, you'd use:

def all_30_or_up(ls):
for i in ls:
if i < 30:
return False
return True

例如,一旦你找到一个值证明 的值小于30,你就返回 False,如果你没有找到相反的证据,你就返回 True

类似地,您可以使用 any()功能来测试 至少1个值是否与条件匹配。

有一个内置的功能 all:

all (x > limit for x in my_list)

限制所有数字必须大于的值。

你可以使用 all():

my_list1 = [30,34,56]
my_list2 = [29,500,43]
if all(i >= 30 for i in my_list1):
print 'yes'
if all(i >= 30 for i in my_list2):
print 'no'

请注意,这包括所有等于或高于30的数字,严格来说不超过30。

你为什么不能用 min()

def above(my_list, minimum):
if min(my_list) >= minimum:
print "All values are equal or above", minimum
else:
print "Not all values are equal or above", minimum

我不知道这是不是你想要的但严格来说,这是你想要的。

你可以这样做:

def Lists():


my_list1 = [30,34,56]
my_list2 = [29,500,43]


for element in my_list1:
print(element >= 30)


for element in my_list2:
print(element >= 30)


Lists()

这将返回大于30的值为 True,小于30的值为 false。

The overall winner between using the np.sum, np.min, and all seems to be np.min in terms of speed for large arrays:

N = 1000000
def func_sum(x):
my_list = np.random.randn(N)
return np.sum(my_list < x )==0


def func_min(x):
my_list = np.random.randn(N)
return np.min(my_list) >= x


def func_all(x):
my_list = np.random.randn(N)
return all(i >= x for i in my_list)

(i need to put the np.array definition inside the function, otherwise the np.min function remembers the value and does not do the computation again when testing for speed with timeit)

The performance of "all" depends very much on when the first element that does not satisfy the criteria is found, the np.sum needs to do a bit of operations, the np.min is the lightest in terms of computations in the general case.

当几乎立即满足条件并且 all 循环快速退出时,all 函数略胜于 np.min:

>>> %timeit func_sum(10)
10 loops, best of 3: 36.1 ms per loop


>>> %timeit func_min(10)
10 loops, best of 3: 35.1 ms per loop


>>> %timeit func_all(10)
10 loops, best of 3: 35 ms per loop

But when "all" needs to go through all the points, it is definitely much worse, and the np.min wins:

>>> %timeit func_sum(-10)
10 loops, best of 3: 36.2 ms per loop


>>> %timeit func_min(-10)
10 loops, best of 3: 35.2 ms per loop


>>> %timeit func_all(-10)
10 loops, best of 3: 230 ms per loop

但是吸毒

np.sum(my_list<x)

可以非常有用的是,我们想知道 x 下面有多少个值。

我写了这个函数

def larger(x, than=0):
if not x or min(x) > than:
return True
return False

然后

print larger([5, 6, 7], than=5)  # False
print larger([6, 7, 8], than=5)  # True
print larger([], than=5)  # True
print larger([6, 7, 8, None], than=5)  # False


最少()上的空列表将引发 ValueError。所以我在条件中添加了 if not x

 a = [[a, 2], [b, 3], [c, 4], [d, 5], [a, 1], [b, 6], [e, 7], [h, 8]]

我需要这个

 a = [[a, 3], [b, 9], [c, 4], [d, 5], [e, 7], [h, 8]]
a.append([0, 0])
for i in range(len(a)):
for j in range(i + 1, len(a) - 1):
if a[i][0] == a[j][0]:
a[i][1] += a[j][1]
del a[j]
a.pop()
        

基于数字阵列和所有函数的解决方案:

my_list1 = [30, 34, 56]
my_list2 = [29, 500, 43]
#
import numpy as np
all(np.array(my_list1)>=30)
Output: True
all(np.array(my_list2)>=30)
Output: False