首页 > 解决方案 > 使用python查找多项式的系数

问题描述

我有以下两个列表:

first = [1,2,3]
second = [6,7,8]

现在我想将两个列表的项目添加到一个新列表中。

输出应该是

three = [6, 7, 8, 12, 14, 16, 18, 21, 24]

标签: pythonlistaddition

解决方案


列表理解

您可以使用此列表理解:

three = [i*j for i in first for j in second]
# [6, 7, 8, 12, 14, 16, 18, 21, 24]

itertools

或者,使用itertools.product(虽然我不确定在这种情况下它会节省你的性能):

from itertools import product

three = [i*j for i,j in product(first,second)]
# [6, 7, 8, 12, 14, 16, 18, 21, 24]

numpy

或者numpy

import numpy as np

three = np.outer(first,second).flatten()
# array([ 6,  7,  8, 12, 14, 16, 18, 21, 24])

推荐阅读