Ask Your Question
0

subtract two data tables

asked 2012-08-08 09:36:06 +0100

b0f00narch gravatar image
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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-08-08 12:56:30 +0100

benjaminfjones gravatar image

Here's a functional style one-liner:

ta3 = map(lambda (x,y): [x[0], x[1]-y[1]], zip(ta1, ta2))
edit flag offensive delete link more
2

answered 2012-08-08 10:50:59 +0100

fidbc gravatar image

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).

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-08-08 09:36:06 +0100

Seen: 351 times

Last updated: Aug 08 '12