我有以下两个列表:
first = [1,2,3]
second = [6,7,8]
现在,我想将两个列表中的项目添加到新列表中。
输出应为
three = [6, 7, 8, 12, 14, 16, 18, 21, 24]
答案 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