计算列表中满足一定条件的值

时间:2021-03-28 07:57:14

I have the following list,

我有以下列表,

mylist = ['0.976850566018849',
 '1.01711066941038',
 '0.95545901267938',
 '1.13665822176679',
 '1.21770587184811',
 '1.12567451365206',
 '1.18041077035567',
 '1.13799827821001',
 '1.1624485106005',
 '1.37823533969271',
 '1.39598077584722',
 '1.23844320976322',
 '1.57397155911713',
 '1.40605782943842',
 '1.36037525085048',
 '1.185',
 '1.22795283469963',
 '1.17192311574904',
 '1.04121940463022',
 '1.0133517787145',
 '0.986161470813006',
 '1.09820439504488',
 '1.06640283661947',
 '1.05764772395448',
 '1.02678616758973',
 '1.01876057166248',
 '1.09019498604372',
 '1.1665479238629',
 '1.07170094763279',
 '1.1326945725342',
 '1.18199297460235',
 '1.20353001964446',
 '1.00973941850665',
 '1.0662943967844',
 '1.04876624296406',
 '1.12447065457189',
 '0.954629674212134',
 '1.02961694279098']

What I want to do is to count how many values in that list which is >= 1.3. Returning 5, which is:

我要做的是计算这个列表中有多少个值>= 1.3。返回5,它是:

      '1.57397155911713' 
      '1.40605782943842'
      '1.36037525085048'
      '1.39598077584722' 
      '1.37823533969271'

Is there a compact way to do it in Python?

在Python中有一种紧凑的方法来实现它吗?

7 个解决方案

#1


11  

I take compactness, you mentioned in the question, as shorter code. So, I present

你在问题中提到的紧凑性,作为更短的代码。所以,我现在

sum(float(num) >= 1.3 for num in mylist)

This takes advantage of the fact that, in python True values are taken as 1 and False as 0. So, whenever float(num) >= 1.3 evaluates to Truthy, it will be 1 and if it fails, result would be 0. So, we add all the values together to get the total number of items which are greater than or equal to 1.3.

这利用了这样一个事实:在python中,True值取1,False取0。因此,每当float(num) >= 1.3计算为Truthy时,它将为1,如果失败,结果将为0。我们把所有的值加起来得到大于或等于1。3的项的总数。

You can check that like this

你可以这样检查

True == 1
# True
True + True
# 2
False * 10
# 0

#2


5  

To count how many values in that list which is >= 1.3:

计算该列表中>= 1.3个值的个数:

sum(1 for x in mylist if float(x) >= 1.3)

If you need to actually extract the list of numbers which satisfy the condition, just create that list with a list comprehension and do whatever you want with it:

如果你真的需要提取满足条件的数字列表,只需创建一个包含列表理解的列表,并使用它做任何你想做的事情:

a = [x for x in mylist if float(x) >= 1.3]
print a
print len(a)

#3


2  

You can use a generator expression

您可以使用生成器表达式

Some simple generators can be coded succinctly as expressions using a syntax similar to list comprehensions but with parentheses instead of brackets. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expressions are more compact but less versatile than full generator definitions and tend to be more memory friendly than equivalent list comprehensions.

一些简单的生成器可以用类似于列表理解的语法简洁地编码为表达式,但是使用圆括号而不是括号。这些表达式是为一个封闭函数立即使用生成器的情况而设计的。生成器表达式比完整的生成器定义更紧凑,但功能更少,而且比等价的列表理解更有利于内存。

Something like this:

是这样的:

sum(1 for x in mylist if float(x) >= 1.3)

#4


1  

If you want the values returned to you and stored in a list you could do:

如果您想要返回值并存储在一个列表中,您可以这样做:

count = []
for value in mylist:
    num = float(value)
    if num >= 1.3:
        count.append(value)

If you want it to output the list just add:

如果要输出列表,只需添加:

print(count)

or if you want the count of the number of values that are greater add:

或者如果你想要更大的数值的计数,可以加上:

print(len(count))

#5


0  

by using list comprehension

通过使用列表理解

>>> len([i for i in mylist if float(i) >= 1.3])

#6


0  

You can use numpy or pandas, though for such a simple computation they would be much slower than the alternatives mentioned above.

您可以使用numpy或panda,尽管对于如此简单的计算,它们将比上面提到的替代方法慢得多。

Using numpy,

使用numpy,

import numpy as np
arr=np.array(mylist).astype(float)
print len(arr[arr>=1.3])

Using pandas,

使用熊猫,

import pandas as pd
s=pd.Series(mylist).astype(float)
print len(s[s>=1.3])

Alternatively,

另外,

(pd.Series(l).astype(float)>=1.3).value_counts()[True]

For performance, the fastest solution seems to be

对于性能,最快的解决方案似乎是

In [51]: %timeit sum(1 for x in mylist if float(x) >= 1.3)
100000 loops, best of 3: 8.72 µs per loop

#7


0  

Here's an alternative, using the reduce() builtin:

这里有一个替代方法,使用reduce()内置:

reduce(lambda x, y: x + (y >= 1.3), mylist, 0)

Just for completeness, since I see there's already an accepted answer. The point is, there's often more than one way to accomplish something in Python (or many other languages, for that matter)...

为了完整起见,我看到已经有了一个公认的答案。关键是,用Python(或许多其他语言)实现某件事通常不止一种方法……

#1


11  

I take compactness, you mentioned in the question, as shorter code. So, I present

你在问题中提到的紧凑性,作为更短的代码。所以,我现在

sum(float(num) >= 1.3 for num in mylist)

This takes advantage of the fact that, in python True values are taken as 1 and False as 0. So, whenever float(num) >= 1.3 evaluates to Truthy, it will be 1 and if it fails, result would be 0. So, we add all the values together to get the total number of items which are greater than or equal to 1.3.

这利用了这样一个事实:在python中,True值取1,False取0。因此,每当float(num) >= 1.3计算为Truthy时,它将为1,如果失败,结果将为0。我们把所有的值加起来得到大于或等于1。3的项的总数。

You can check that like this

你可以这样检查

True == 1
# True
True + True
# 2
False * 10
# 0

#2


5  

To count how many values in that list which is >= 1.3:

计算该列表中>= 1.3个值的个数:

sum(1 for x in mylist if float(x) >= 1.3)

If you need to actually extract the list of numbers which satisfy the condition, just create that list with a list comprehension and do whatever you want with it:

如果你真的需要提取满足条件的数字列表,只需创建一个包含列表理解的列表,并使用它做任何你想做的事情:

a = [x for x in mylist if float(x) >= 1.3]
print a
print len(a)

#3


2  

You can use a generator expression

您可以使用生成器表达式

Some simple generators can be coded succinctly as expressions using a syntax similar to list comprehensions but with parentheses instead of brackets. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expressions are more compact but less versatile than full generator definitions and tend to be more memory friendly than equivalent list comprehensions.

一些简单的生成器可以用类似于列表理解的语法简洁地编码为表达式,但是使用圆括号而不是括号。这些表达式是为一个封闭函数立即使用生成器的情况而设计的。生成器表达式比完整的生成器定义更紧凑,但功能更少,而且比等价的列表理解更有利于内存。

Something like this:

是这样的:

sum(1 for x in mylist if float(x) >= 1.3)

#4


1  

If you want the values returned to you and stored in a list you could do:

如果您想要返回值并存储在一个列表中,您可以这样做:

count = []
for value in mylist:
    num = float(value)
    if num >= 1.3:
        count.append(value)

If you want it to output the list just add:

如果要输出列表,只需添加:

print(count)

or if you want the count of the number of values that are greater add:

或者如果你想要更大的数值的计数,可以加上:

print(len(count))

#5


0  

by using list comprehension

通过使用列表理解

>>> len([i for i in mylist if float(i) >= 1.3])

#6


0  

You can use numpy or pandas, though for such a simple computation they would be much slower than the alternatives mentioned above.

您可以使用numpy或panda,尽管对于如此简单的计算,它们将比上面提到的替代方法慢得多。

Using numpy,

使用numpy,

import numpy as np
arr=np.array(mylist).astype(float)
print len(arr[arr>=1.3])

Using pandas,

使用熊猫,

import pandas as pd
s=pd.Series(mylist).astype(float)
print len(s[s>=1.3])

Alternatively,

另外,

(pd.Series(l).astype(float)>=1.3).value_counts()[True]

For performance, the fastest solution seems to be

对于性能,最快的解决方案似乎是

In [51]: %timeit sum(1 for x in mylist if float(x) >= 1.3)
100000 loops, best of 3: 8.72 µs per loop

#7


0  

Here's an alternative, using the reduce() builtin:

这里有一个替代方法,使用reduce()内置:

reduce(lambda x, y: x + (y >= 1.3), mylist, 0)

Just for completeness, since I see there's already an accepted answer. The point is, there's often more than one way to accomplish something in Python (or many other languages, for that matter)...

为了完整起见,我看到已经有了一个公认的答案。关键是,用Python(或许多其他语言)实现某件事通常不止一种方法……