python - python中列表列表之间的叉积

标签 python python-3.x list

<分区>

假设我有如下 2 个列表:

list1=[[1],[2],[3]]
list2=[[4],[5]]

我想做的是这两个列表中的列表项之间的叉积。在这个例子中,结果将是:

    result_list=[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]

如何使用 python 完成此操作?我在网上搜索了一下,但找不到解决方案,我一直被卡住了。非常欢迎任何帮助。

提前致谢!

最佳答案

你可以试试 itertools.product

from itertools import product
list1=[[1],[2],[3]]
list2=[[4],[5]]

output = [[x[0][0], x[1][0]] for x in product(list1, list2)]

print(output)
[[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

关于python - python中列表列表之间的叉积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66517274/

相关文章:

python - App Engine 在 dev_appserver.py 中找不到 google.auth 文件

python - 安装和导入后,colaboratory 将不承认 arviz

python - 使用命令 `osascript -e ' quit app "Quicktime Player 7"'` 与 python

python - TypeError : 'function' object is not iterable' Python 3

python - 在 python 中使用 id() 比较列表(整数)

python - 初始化与数组形状相同的空列表

c# - 从列表中删除特定元素

python - 我在 seaborn import : 中面临这个问题

python - 在 `for` 中使用 `print()` 会在 Python 3.x 上生成生成器吗?

python - Pandas iterrows 只给出最后一行的结果