简体   繁体   中英

How to assign value from list?

i have tuples in a list :

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

i want to assign all value in list, example:

A = [1,6], B = [8,2]

A = [8,2], B = [6,3]

then perform calculations between the elements together and print the results on the screen

C1 = (A[1]+B[1],A[2]+B[2])

C2 = (A[1]+B[1],A[2]+B[2])

Thank you!!!

X = [1,8,6,9,5]

Y = [6,2,3,4,7]

res = list(zip(X,Y))

a = list(zip(res, res[1:]))

print(a)

I can't think how to assign A and B in list

This list comprehension should perform the operations you described

>>> [tuple(sum(i) for i in zip(x, y)) for x, y in a]
[(9, 8), (14, 5), (15, 7), (14, 11)]
>>> a = [((1, 6), (8, 2)), ((8, 2), (6, 3)), ((6, 3), (9, 4)), ((9, 4), (5, 7))]
>>> A = a[0][0] # First item's ((1,6), (8,2)) first item (1,6)
>>> B = a[0][1] # First item's ((1,6), (8,2)) second item (8,2)
>>> A
(1, 6)
>>> B
(8, 2)
>>>

using lambda

a = [((1, 6), (8, 2)), ((8, 2), (6, 3)), ((6, 3), (9, 4)), ((9, 4), (5, 7))]
sol = list(map(lambda x:(x[0][0]+x[1][0],x[0][1]+x[1][1]), a))
print(sol)

output

[(9, 8), (14, 5), (15, 7), (14, 11)]

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