Specify the number of values ​​in the list that satisfy a specific condition

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 calculate how many values ​​in this list> = 1.3. Refund 5, which:

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

Is there a compact way to do this in Python?

+6
source share
7 answers

I accept compactness, you mentioned in the question, as a shorter code. So i present

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

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

You can check that like this

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

To calculate how many values ​​in this list> = 1.3:

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

If you really need to extract a list of numbers that satisfy the condition, just create this list with a list and do whatever you want:

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

You can use expression

Some simple generators can be concisely encoded as expressions using syntax similar to a list, but with parentheses instead of brackets. These expressions are intended for situations when the generator is immediately used by the close function. Generator expressions are more compact , but less versatile than a full definition generator, and tend to be more memory friendly than an equivalent comprehension list.

Something like that:

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

If you need the values ​​returned to you and stored in a list that you could do:

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

If you want it to list, just add:

 print(count) 

or if you want the number of additional values ​​to be greater:

 print(len(count)) 
+1
source

using list comprehension

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

You can use numpy or pandas , although for such a simple calculation they will be much slower than the alternatives mentioned above.

Using 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]) 

As an alternative,

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

For performance, the fastest solution is similar to

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

Here's an alternative using reduce() builtin:

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

Just for completeness, as I see there already accepted answer. The thing is, in Python (or in many other languages, for that matter) there is more than one way to do something ...

0
source

All Articles