使用python查找多项式系数

时间:2018-10-01 02:03:48

标签: python list addition

我有以下两个列表:

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

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

输出应为

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

1 个答案:

答案 0 :(得分:2)

列表理解

您可以使用此列表理解:

new = []
for i in extract:
    if i in list(word):
        new.append(i)

new = ''.join(new)
print(new)

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