문제

I am trying to multiply all combinations of the numbers in 2 lists in python.

For Example

list(abc) and list(xyz) would return a*x a*y a*z b*x b*y b*z c*x c*y c*z

I am quite new to python and so I do not have much experience.

도움이 되었습니까?

해결책

How about this :

l1 = [1, 2, 3]
l2 = [10, 100, 1000]
l3 = [ x*y for x in l1 for y in l2]

다른 팁

Use the inbuilt zip function

l1 = [1, 2, 3]  
l2 = [10, 100, 1000]  
l3 = [i * j for i, j in zip(l1, l2)]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top