python - Updating a python array without loops?
问题描述
I have a recursive relation, that I want to update without using loops for speed.
For example
import numpy as np
x=np.array([1,0,0,0,0])
x[1:]=(1+x[0:len(x)-1])**2.
This returns [1,4,1,1,1]
for x
, but I want [1,4,25,676,458329]
. I know this can be done with a loop, but I'm really trying to avoid loops.
For example,
for i in range(1,len(x)):
x[i]=(1+x[i-1])**2.
will return [1,4,25,676,458329]
解决方案
>>> import numpy as np
>>> x=np.array([1,0,0,0,0])
>>> x[1:]=1
>>> x
array([1, 1, 1, 1, 1])
推荐阅读
- css - 无法从反应光滑中获得所需的结果
- outlook - Outlook 日历:将同一邮箱的不同日历添加到事件中
- kotlin - 如何正确设置 Viewmodel 类?
- python - 忽略 Popen 的返回值是否安全?
- docker - *1 "/usr/share/nginx/html/" 的目录索引是禁止的,在 mac catalina os 中
- asp.net-core - 如何在 ASP.Net Core 3.1 中使用 2 行标题动态更新表中第一行标题的跨度和内容?
- database - 数据库上的 CI/CD 实现
- node.js - NodeJS - 如何使用 node-opcua-pki 生成证书和私钥
- django - 重定向,无法离开登录页面
- powershell - Powershell Umlaute 检查始终显示错误