Python: Find a value in list -
def sum_points(point_list): sol = [] sum = 0.0 x in point_list: sum += x sol.append(sum) return sol def main(): open("example.json") f: line in f: result = json.loads(line) if('text' , 'place' in result.keys()) , (type(result['place']) dict): place_user = result['place'] tw_user = result['text'] words = tw_user.encode("utf-8").split() points = dict.fromkeys(words,0) open("wordlist.txt") f: line in f: key, val = line.split("\t") if key in points: points[key.strip()] += int(val) point_list = sum_points(points.values()) addr_code = place_user['full'].encode("utf-8").split()
from code, got 2 data. first point_list. it's list type of data below.
[1.0] [3.0] [0.0] [2.0] [1.0]
and, other addr_code. it's list type of data below.
['ca'] ['nj'] ['dc'] ['nj'] ['ri']
these 2 data show information these. 'ca' = 1.0, 'nj' = 3.0, 'dc' = 0.0, 'nj'= 2.0, 'ri'=1.0
from these data, want simple result below.
nj has biggest point: 5.0
how write code last part result? , please tell me if there wrong part. i'll waiting help.
use collections.defaultdict
>>> lis2=['ca','nj','dc','nj','ri'] >>> lis1 =[1.0,3.0,0.0,2.0,1.0] >>> collections import defaultdict >>> dic = defaultdict(float) >>> x,y in zip(lis2,lis1): ... dic[x] += y ... >>> maxx = max(dic,key =dic.get) >>> "{0} has biggest point: {1}".format(maxx,dic[maxx]) 'nj has biggest point: 5.0'
Comments
Post a Comment