[英]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 我不认为如何在列表中分配A和B
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
使用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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.