python - merge a few lists and regroup them into a dictionary to plot to multi-series Highcharts -
animal = ['hamster', 'dolphin', 'ant', 'hamster', 'hamster', 'ant'] date = [20151007, 20151007, 20151007, 20141007, 20120101, 20010101] total = [27, 5, 5, 5, 18, 40]   i want combine these 3 list one. if animal same, should grouped , total should follow asc date. if animal type doesn't have enough date, it'll become 0. example:
[{'name': 'hamster', 'data': [0, 18, 5, 27]},  {'name': 'dolphin', 'data': [0, 0, 0, 5]},  {'name': 'ant', 'data': [40, 0, 0, 5]}]   i'm trying plot them highcharts dates in own list.
[20010101, 20120101, 20141007, 20151007]    i can combine lists using:
zip(animal, date, total)   but how them it's own type , arrange total asc date?
edited
this how got far. don't think it's efficient. think? improvement suggest? (only pythonic suggestion pls)
b = [] k, v in enumerate(animal):     counter = 0     in range(len(b)):         try:             if b[i]['name'] == v:                 b[i]['data'].append((date[k], total[k]))                 counter = counter + 1         except keyerror:             continue     if counter == 0:         b.append({'name': v,  'data': [(date[k], total[k])]})  test = set(date)  k, v in enumerate(list(ordereddict.fromkeys(animal))):     x in test:         try:             in range(len(test)):                 if b[k]['data'][i][0] == x:                     break         except indexerror:             b[k]['data'].append((x, 0))     b[k]['data'].sort(key=itemgetter(0))     temp = [x[1] x in b[k]['data']]     b[k]['data'] = []     b[k]['data'] = temp   result:
[{'data': [0, 18, 5, 27], 'name': 'hamster'},  {'data': [0, 0, 0, 5], 'name': 'dolphin'},  {'data': [40, 0, 0, 5], 'name': 'ant'}]      
i think want this:
animals = ['hamster', 'dolphin', 'ant', 'hamster', 'hamster', 'ant'] dates = [20151007, 20151007, 20151007, 20141007, 20120101, 20010101] totals = [27, 5, 5, 5, 18, 40] readings = list(zip(animals, dates, totals)) dates = set(dates) data = dict() animal in animals:     data[animal] =  {(date, total) (x, date, total) in readings if x == animal}     missingdates = dates - {d[0] d in data[animal] }     data[animal] |= {(date, 0) date in missingdates}    at point, data is
{'dolphin': {(20120101, 0), (20151007, 5), (20141007, 0), (20010101, 0)}, 'hamster': {(20151007, 27), (20141007, 5), (20120101, 18), (20010101, 0)}, 'ant': {(20120101, 0), (20151007, 5), (20141007, 0), (20010101, 40)}}
and need pull apart , sort data each animal date. hope helps.
edit here's further explanation promised. unfortunately can't see comment , edit screen @ same time; hope answer points raised. after zipping 3 lists together, need extract data each animal. expression on right-hand side of
data[animal] =  {(date, total) (x, date, total) in readings if x == animal}   is set comprehension, , gives set of (date, total) pairs related a specific animal. need find missing dates. used set difference this:
missingdates = dates - {d[0] d in data[animal] }     this why used sets: able write expression missing dates succinctly. instead, have used lists:
data[animal] = [(date, total) (x, date, total) in readings if x == animal] missingdates = [d d in dates if d not in [d[0] d in data[animal]]   if had done this, wouldn't have needed earlier statement
dates = set(dates)    but wanted avoid convoluted expression missingdates, , didn't want write
presentdates = [d[0] d in data[animal]  missingdates = [d d in dates if d not in presentdates]   now have add pairs (date, 0) missing dates. if had used lists, write
data[animal] += [(date, 0) date in missingdates]    but + operation isn't defined sets; need used union.  (the union of 2 sets set of elements belong @ least 1 of 2 sets.)  union operation represented as|.  you're correct can represent bitwise or of 2 integers, can represent other operations well, + mean addition of number or concatenation of lists.  so:
data[animal] |= {(date, 0) date in missingdates}   if you're not comfortable sets, means use lists instead, should make point of learning sets. they're extremely useful.
to continue past point wrote earlier, need sort data[animal] date. here have change lists, because sets unsorted.
data[animal] = list(data[animal]) data[animal].sort() data[animal] = [d[1] d in data[animal]]   of course, if elect use lists throughout, don't need first of these lines.
i hope clears you. let me know if have further questions.
Comments
Post a Comment