Average of string & missing values in dictionary (python) -
i have dictionary whereas values of string type. want calculate average per key whereas empty string missing value. here's code need small twerk work...with error : int() argument must string or number, not 'list'
dic = {'e': ['1', '', '3'], 'm': ['2', '', '1']} k,n in dic.items(): k = [0] n = [1] d = {k: (int(n)) k, n in dic.items() if n} ave = {k:sum(d)/float(len(d))} if d else '-' print ave
expected output: {'e': 2, 'm': 1.5}
dic = {'e': ['1', '', '3'], 'm': ['2', '', '1']} ave = dict() k in dic: nonempty = [ float(v) v in dic[k] if v != '' ] ave[k] = sum(nonempty) / len(nonempty) print ave
Comments
Post a Comment