python - 使用python查找多项式的系数
问题描述
我有以下两个列表:
first = [1,2,3]
second = [6,7,8]
现在我想将两个列表的项目添加到一个新列表中。
输出应该是
three = [6, 7, 8, 12, 14, 16, 18, 21, 24]
解决方案
列表理解
您可以使用此列表理解:
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])
推荐阅读
- mysql - MySQL 意外关闭
- php - 我的代码没有使用“取消链接”(debian 10)删除我的 php 会话
- arrays - 具有给定总和的子数组计数,使得索引按升序排列
- python - 从完整的 txt 文件文件夹创建多个或单个 csv 文件
- python - 如何在 Django 中建模财务会计系统?
- javascript - 当页面/浏览器空闲时保持套接字活动
- android - 加载主页组件时崩溃本机反应
- python - 如何重写打字机效果功能以使其适用于程序输出的任何文本?
- java - 附近连接:Android 10 上的发现问题
- flutter - Flutter webview显示空白屏幕