Count how many values in a list that satisfy certain condition

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

I have the following list,

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


        
7条回答
  •  猫巷女王i
    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)
    

提交回复
热议问题