Amanda
Amanda

Reputation: 2143

Comparing 2 list of tuples

I have a list :

 a = [(1, 2), (3, 4), (4, 5), (6, 7)]
 # Stores list of x,y coordinates

and a list:

 b = [(1, 2), (10, 1), (3, 10), (4, 9)]

Now, I want to replace in a where it has y coordinate in a >= of , b with y coordinate + 2.

Since here a has an equivalent or greater of b in:

[(1,2), (3,4)]

I want to replace in a such that it becomes:

a = [(1,4), (3,6), (4,5), (6,7)]

How could I do this?

I know there exists a method with numpy such that:

np.where(a >= b) , do something;

but not sure how could I use it in this case.

Upvotes: 0

Views: 70

Answers (2)

jfaccioni
jfaccioni

Reputation: 7509

no numpy:

a = [(1, 2), (3, 4), (4, 5), (6, 7)]
b = [(1, 2), (10, 1), (3, 10), (4, 9)]
c = [(aa[0], aa[1]+2) if aa[1] >= bb[1] else aa for aa, bb in zip(a, b)]

c is [(1, 4), (3, 6), (4, 5), (6, 7)]

Upvotes: 0

rafaelc
rafaelc

Reputation: 59274

IIUC, compare their axis=1 and +=2

a = np.asarray(a)
b = np.asarray(b)

a[a[:, 1] > b[:, 1], 1] += 2

array([[1, 2],
       [3, 6],
       [4, 5],
       [6, 7]])

Upvotes: 2

Related Questions