Multiplying all combinations of values of two lists [closed]

拥有回忆 提交于 2020-02-23 08:34:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!