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.
如何检查列表中的所有值是否> = 30? my_list1应该工作,my_list2不应该。
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:
事后看来,在处理速度真正重要且利用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
You could also do something like:
你也可以这样做:
len([*filter(lambda x: x >= 30, my_list1)]) > 0
7 个解决方案
#1
76
Use the all()
function with a generator expression:
将all()函数与生成器表达式一起使用:
>>> 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.
请注意,此测试大于或等于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
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.
例如一旦找到证明值低于30的值,就返回False,如果没有找到相反的证据,则返回True。
Similarly, you can use the any()
function to test if at least 1 value matches the condition.
同样,您可以使用any()函数来测试至少1个值是否与条件匹配。
#2
5
...any reason why you can't use min()
?
...任何你不能使用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
I don't know if this is exactly what you want, but technically, this is what you asked for...
我不知道这是不是你想要的,但从技术上讲,这就是你要求的......
#3
3
There is a builtin function all
:
内置功能全部:
all (x > limit for x in my_list)
Being limit the value greater than which all numbers must be.
限制值大于所有数字必须的值。
#4
2
You can use all()
:
你可以使用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'
Note that this includes all numbers equal to 30 or higher, not strictly above 30.
请注意,这包括所有等于30或更高的数字,而不是严格地高于30。
#5
0
You could do the following:
您可以执行以下操作:
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()
This will return the values that are greater than 30 as True, and the values that are smaller as false.
这将返回大于30的值为True,以及小于false的值。
#6
0
The overall winner between using the np.sum, np.min, and all seems to be np.min in terms of speed for large arrays:
在大型数组的速度方面,使用np.sum,np.min和all之间的总体赢家似乎是np.min:
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)
(我需要将np.array定义放在函数中,否则np.min函数会记住该值,并且在使用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”的性能在很大程度上取决于何时找到不满足条件的第一个元素,np.sum需要做一些操作,np.min在一般情况下的计算方面是最轻的。
When the criteria is almost immediately met and the all loop exits fast, the all function is winning just slightly over np.min:
当几乎立即满足标准并且全部循环快速退出时,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:
但是当“所有”需要通过所有点时,它肯定会更糟,并且np.min获胜:
>>> %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
But using
但是使用
np.sum(my_list<x)
can be very useful is one wants to know how many values are below x.
可能非常有用,是想知道有多少值低于x。
#7
0
I write this function
我写这个功能
def larger(x, than=0):
if not x or min(x) > than:
return True
return False
Then
然后
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
Empty list on min() will raise ValueError. So I added if not x
in condition.
min()上的空列表将引发ValueError。所以我添加了条件不是x。
#1
76
Use the all()
function with a generator expression:
将all()函数与生成器表达式一起使用:
>>> 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.
请注意,此测试大于或等于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
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.
例如一旦找到证明值低于30的值,就返回False,如果没有找到相反的证据,则返回True。
Similarly, you can use the any()
function to test if at least 1 value matches the condition.
同样,您可以使用any()函数来测试至少1个值是否与条件匹配。
#2
5
...any reason why you can't use min()
?
...任何你不能使用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
I don't know if this is exactly what you want, but technically, this is what you asked for...
我不知道这是不是你想要的,但从技术上讲,这就是你要求的......
#3
3
There is a builtin function all
:
内置功能全部:
all (x > limit for x in my_list)
Being limit the value greater than which all numbers must be.
限制值大于所有数字必须的值。
#4
2
You can use all()
:
你可以使用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'
Note that this includes all numbers equal to 30 or higher, not strictly above 30.
请注意,这包括所有等于30或更高的数字,而不是严格地高于30。
#5
0
You could do the following:
您可以执行以下操作:
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()
This will return the values that are greater than 30 as True, and the values that are smaller as false.
这将返回大于30的值为True,以及小于false的值。
#6
0
The overall winner between using the np.sum, np.min, and all seems to be np.min in terms of speed for large arrays:
在大型数组的速度方面,使用np.sum,np.min和all之间的总体赢家似乎是np.min:
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)
(我需要将np.array定义放在函数中,否则np.min函数会记住该值,并且在使用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”的性能在很大程度上取决于何时找到不满足条件的第一个元素,np.sum需要做一些操作,np.min在一般情况下的计算方面是最轻的。
When the criteria is almost immediately met and the all loop exits fast, the all function is winning just slightly over np.min:
当几乎立即满足标准并且全部循环快速退出时,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:
但是当“所有”需要通过所有点时,它肯定会更糟,并且np.min获胜:
>>> %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
But using
但是使用
np.sum(my_list<x)
can be very useful is one wants to know how many values are below x.
可能非常有用,是想知道有多少值低于x。
#7
0
I write this function
我写这个功能
def larger(x, than=0):
if not x or min(x) > than:
return True
return False
Then
然后
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
Empty list on min() will raise ValueError. So I added if not x
in condition.
min()上的空列表将引发ValueError。所以我添加了条件不是x。