Count how many values in a list that satisfy certain condition

前端 未结 7 1423
醉话见心
醉话见心 2021-01-11 13:50

I have the following list,

mylist = [\'0.976850566018849\',
 \'1.01711066941038\',
 \'0.95545901267938\',
 \'1.13665822176679\',
 \'1.21770587184811\',
 \'1.         


        
相关标签:
7条回答
  • 2021-01-11 13:54

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

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

    0 讨论(0)
  • 2021-01-11 13:55

    You can use numpy or pandas, though for such a simple computation they would 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])
    

    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
    
    0 讨论(0)
  • 2021-01-11 13:59

    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.

    You can check that like this

    True == 1
    # True
    True + True
    # 2
    False * 10
    # 0
    
    0 讨论(0)
  • 2021-01-11 14:04

    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))
    
    0 讨论(0)
  • 2021-01-11 14:07

    by using list comprehension

    >>> len([i for i in mylist if float(i) >= 1.3])
    
    0 讨论(0)
  • 2021-01-11 14:15

    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)
    
    0 讨论(0)
提交回复
热议问题