简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM