[英]multiply all elements in a list of tuples for elements in a list
I know it might sound easy but that problem is making me struggle.我知道这听起来很容易,但这个问题让我很挣扎。
I have a list of tuple as我有一个元组列表
[(0,1),(3,4)]
And I want to multiply all its values for all the values in a list like我想将其所有值乘以列表中的所有值,例如
[ 1, 2, 3]
So That in this case I will get a new list (nested) of tuples:所以在这种情况下,我将得到一个新的元组列表(嵌套):
[[(0,1),(0,2),(0,3)],[(3,4),(6,8),(9,12)]]
How can I obtain this result?我怎样才能得到这个结果?
Using a list comprehension:使用列表推导:
tuples = [(0,1),(3,4)]
multipliers = [1, 2, 3]
results = [[(t[0]*m, t[1]*m) for m in multipliers] for t in tuples]
nested loops.嵌套循环。
result = []
for t in tuples:
for m in multipliers:
result.append( (t[0]*m, t[1]*m) )
print(result)
gives:给出:
[(0, 1), (0, 2), (0, 3), (3, 4), (6, 8), (9, 12)]
This is a list of tuples (the inner list).这是一个元组列表(内部列表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.