Average of a list of numbers, stored as strings in a Python list

后端 未结 5 1920
暖寄归人
暖寄归人 2020-12-20 04:56

I want to calculate the average value of several lists in python. These lists contain numbers as strings. Empty string isn\'t zero, it means a missing value.

The be

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 05:32

    In Python 3.4 use the statistics library:

    from statistics import mean
    num = ['1', '2', '', '6']
    ave = mean(int(n) if n else 0 for n in num)
    

提交回复
热议问题