python - Combine data from multiple lists of different length -


i have db query returning 3 sets of data. comes in format.

year = (('adison', '355', 4), ('windsor windham', '455', 6), ('windham', '655', 2), ('btown', '233', 5)) month = (('adison', '355', 2), ('windham', '655', 1)) week = (('btown', '233', 8), ('adison', '355', 9)) 

the year list longest values. need take last value each element in month , week lists , append them year list in proper spot, based on town.

if there not corresponding value in month or week, need append 0. ideally making this:

year = (('adison', '355', 4, 2, 9), ('windsor windham', '455', 6, 0, 0), ('windham', '655', 2, 1, 0), ('btown', '233', 5, 0, 8)) 

i have tried putting 2 lists in loop , using if in conditional check values think overlooking , getting index errors. tried this:

for each in year:     part in month:         if part in each:             each.append(part[-1])         else:             each.append(0) 

i know there has better way, , 1 works accomplish this. there tool or module should looking into? have played zip because not same length having trouble. thank you!

edit

i understand have tuples above, in code convert them list objects before modifying. on python 3.6

first, key dict off of first 2 elements, using zeros default month , week. fill in month , week, if necessary:

data = {(name, n): [y, 0, 0] name, n, y in year}  name, n, m in month:     data[name, n][1] = m  name, n, w in week:     data[name, n][2] = w  data = tuple(tuple([*k, *v]) k, v in data.items()) 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -