Count how many values in a list that satisfy certain condition

前端 未结 7 1414
醉话见心
醉话见心 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 14:19

    To count how many values in that list which is >= 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)
    

提交回复
热议问题