python - 使用列表作为函数输入时的输出列表理解
问题描述
我有一个函数func
,它有一个nxn数组和一个线性空间np.arange(0,10,1)
作为输入。
如何同时遍历这个数组和线性空间,以创建一个数组,其中行是func
线性空间中的返回值?
作为 MWE:
import numpy as np
def func(x,t):
return t+x
ts = [[1,2],
[5,6],
[9,10]]
x_axis = np.arange(0,5,~1)
empty = [[] for i in range(3)]
for t, j in enumerate(ts):
for th in j:
empty[t] = []
for x in x_axis:
empty[t].append(func(x,t))
print(empty)
输出:
[[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]
期望输出:
[[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[5, 6, 7, 8, 9],
[6, 7, 8, 9, 10],
[9, 10, 11, 12, 13],
[10, 11, 12, 13, 14]]
我可能将数组与列表混合在一起,并且可能 append() 不是这项工作的方法。谢谢!
解决方案
推荐阅读
- apl - 是否可以在函数中返回 lambda?
- python - 从 JS 到烧瓶的 JSON 数据返回 None
- node.js - 如何使用 Node 将电子邮件添加到 SendGrid 中的联系人列表?
- python - Python:在“if”函数中定义并在另一个函数中使用时未检测到变量
- javascript - 下拉菜单在单击模式中的下拉滚动条时关闭,但在 React 中的模式之外使用时可以正常工作吗?
- java - 单击重新启动按钮后如何在自定义视图类中重绘画布
- pure-function - 关于纯函数中的“可测试结果”
- python - 无法使用 Python Selenium 和 shadowRoot.querySelector() 定位特定元素
- c++ - 比 case 语句更快速地对行和列信息进行简单跟踪
- python - 如何将 {{ }} 从数据库发送到烧瓶 jinja2