0

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

4
  • for (A, B) in a: and then you do what you want with them... Commented May 25, 2019 at 22:11
  • if tuples has 100 value, i can't do that by hand, we must use "for" Commented May 25, 2019 at 22:30
  • Can you details ??? Commented May 25, 2019 at 22:47
  • What exactly do you expect to be the result of what you are doing? And why do you have such strange data as input. The B in each tuple is the same as A in the next, so you have redundancy which helps nothing. The correct input data without redundancy would be [(1, 6), (8, 2), (6, 3), (9, 4), (5, 7)]. Commented May 26, 2019 at 17:15

3 Answers 3

1

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)]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.