检查列表中的所有值是否都大于某个数字 [英] Check if all values in list are greater than a certain number

查看:134
本文介绍了检查列表中的所有值是否都大于某个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何检查列表中的所有值是否大于等于30? my_list1 应该可以工作, my_list2 不应该。我只能想到的是:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $对于k中的ls:
如果k> = 30:
boolean = boolean + 1
else:
boolean = 0
if boolean> 0:
print'Continue'
elif boolean = 0:
pass



更新2016年:



事后看来,在处理速度非常重要的大型数据集并利用 numpy 之后。 ..我会这样做:

 >>> 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

你也可以这样做:

  len([* filter(lambda x:x > = 30,my_list1)])> 0 


解决方案

使用 all()函数: / p>

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

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



<如果你想在一个函数中做到这一点,你可以使用:

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

例如。只要找到一个值<30>的值,就会返回 False ,并返回 True 如果您发现没有相反的证据。



同样,您可以使用 any()函数来测试至少1 值符合条件。


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

解决方案

Use the all() function 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

Note that this tests for greater than or equal to 30, otherwise my_list1 would not pass the test either.

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

e.g. as soon as you find a value that proves that there is a value below 30, you return False, and return True if you found no evidence to the contrary.

Similarly, you can use the any() function to test if at least 1 value matches the condition.

这篇关于检查列表中的所有值是否都大于某个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆