问题
I want to multiply each element of a list by each element of a second list.
Example:
a = [1, 2, 3]
b = [2, 3, 4]
ILookFor = [2, 4, 6, 3, 6, 9, 4, 8, 12]
How can I get the desired result?
回答1:
This is how you would do it:
a = [1, 2, 3]
b = [2, 3, 4]
result = [c * d for c in b for d in a]
print(result)
# [2, 4, 6, 3, 6, 9, 4, 8, 12]
回答2:
a = [1, 2, 3]
b = [2, 3, 4]
result = [i * j for i in b for j in a]
# [2, 4, 6, 3, 6, 9, 4, 8, 12]
来源:https://stackoverflow.com/questions/45882842/multiplying-all-combinations-of-values-of-two-lists