python - Dynamically get the count of partial no. of fields iterating through list of lists -


i have data file has unit prices 1 4 along other fields. of rows 4 unit prices given , few of unit prices empty. tried calculating average unit price each row replacing space 0 when doing average ignore unit price space , divide remaining count of items.

input:-

10 20 30 40 30    50 60 20    10  

code :-

round(sum( float(v) if v else 0.0 v in i[2:6])/4,2) 

output :-

25 46.66 15 

is there way count of values in list ignoring spaces? inputs appreciated. thanks.

you can filter out empty entries , calculate mean using python's standard statistics library.

import statistics  round(statistics.mean(float(x) x in i[2:6] if x), 2) 

Comments