merge lists in python adding elements repeated -


i have question, have 2 files, each of them have 2 columns, 1 t , second column function depending on t. want merge them, , write them both in output file, using following (for reason using t = t1+t2 didn't work , that's why i'm using extend).

t1 = column1_of_file1 y1 = column2_of_file1 t2 = column1_of_file2 y2 = column2_of_file2  total_t = [] total_y = [] total_t.extend(t1) total_t.extend(t2) total_y.extend(y1) total_y.extend(y2) 

the problem have there elements in t1 same ones of t2. t1 starts 1 , finishes in 4, i'm listing below last 7 elements of both columns:

# t1       y1  ...        ... 3.76    -25.8529     3.80    -25.8474    3.84    -25.8422   3.88    -25.8356   3.92    -25.8286   3.96    -25.8133  4.00    -25.7997    

and t2 starts values 3.80 , runs 8, corresponding values y2 different:

#t2        y2 3.80    -25.7331 3.84    -25.0383 3.88    -24.4059 3.92    -23.8288 3.96    -23.3027  4.00    -22.8242   4.04    -22.3917  ...       ... 

what want merge both lists removing repeated elements in t (which know how t1 in t2 remove) since values in y different want add them up, in end t_total , y_total like:

#t_total    y_total 3.80        y1[3.80] + y2[3.80]  3.84        y1[3.84] + y2[3.84] 3.88        y1[3.88] + y2[3.88] 3.92        y1[3.92] + y2[3.92] 3.96        y1[3.96] + y2[3.96] 4.00        y1[4.00] + y2[4.00] 

any ideas? length of t1 , t2 different 1 y1, y2.

you can collections.defaultdict.

keeping values of y1 , y2

assuming want have list of y1 , y2 works:

from collections import defaultdict  text1='''3.76    -25.8529 3.80    -25.8474 3.84    -25.8422 3.88    -25.8356 3.92    -25.8286 3.96    -25.8133 4.00    -25.7997 ''' text2 = '''3.80    -25.7331 3.84    -25.0383 3.88    -24.4059 3.92    -23.8288 3.96    -23.3027 4.00    -22.8242 4.04    -22.3917''' f1 = {line.split()[0] : line.split()[1] line in text1.splitlines()} f2 = {line.split()[0] : line.split()[1] line in text2.splitlines()} f_total = defaultdict(list) key,value in f1.items():     f_total[key].append(value) key,value in f2.items():     f_total[key].append(value) print (f_total) 

output

defaultdict(<class 'list'>, { '3.76': ['-25.8529'],  '4.00': ['-25.7997', '-22.8242'],  '4.04': ['-22.3917'],  '3.80': ['-25.8474', '-25.7331'],  '3.88': ['-25.8356', '-24.4059'],  '3.92': ['-25.8286', '-23.8288'],  '3.84': ['-25.8422', '-25.0383'],  '3.96': ['-25.8133', '-23.3027']}) 

aggregation

if want add them change defaultdict type float:

f1 = {line.split()[0] : float(line.split()[1]) line in text1.splitlines()} f2 = {line.split()[0] : float(line.split()[1]) line in text2.splitlines()} f_total = defaultdict(float) key,value in f1.items():     f_total[key]+=value key,value in f2.items():     f_total[key]+=value print (f_total) 

output

defaultdict(<class 'float'>, {'4.00': -48.623900000000006, '3.96': -49.116, '3.76': -25.8529, '3.80': -51.5805, '3.92': -49.6574, '3.84': -50.8805, '4.04': -22.3917, '3.88': -50.2415}) 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -