Ask Your Question

# subtract two data tables

ta1=[[1,1],[2,2],[3,3]]
ta2=[[1,1],[2,1],[3,1]]

ta1_j=[]
ta2_j=[]
for i,j in ta1:
ta1_j.append(j)
for i,j in ta2:
ta2_j.append(j)

ta3_j=[]
for i,j in zip(ta1_j,ta2_j):
ta3_j.append(i-j)

ta3_i=[]
for i,j in ta1:
ta3_i.append(i)
ta3=zip(ta3_i,ta3_j)
ta3


I want to subtract table2 from table1 in such way that X stays the same and only Y values are subtracted. The above code works for me, but is there a nicer, better and easier way to subtract tables ?

edit retag close merge delete

## 2 answers

Sort by ยป oldest newest most voted

Here's a functional style one-liner:

ta3 = map(lambda (x,y): [x[0], x[1]-y[1]], zip(ta1, ta2))

more

Here is a one liner for that.

sage: ta3 =[ [ta1[i][0],ta1[i][1]-ta2[i][1]] for i in range(len(ta1))]


Assuming len(ta1)==len(ta2).

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2012-08-08 02:36:06 -0500

Seen: 125 times

Last updated: Aug 08 '12