gpt4 book ai didi

python - 在 numpy 数组上查看

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:10 25 4
gpt4 key购买 nike

我有一个矩阵

 m = np.zeros((5,5))

最后一行可以看到什么

row = m[-1]

但是如果我向 m 添加一列:

m = np.c_[m[:,:2],[0,0,1,1,1],m[:,2:]]

并打印出行,但我没有得到新列。

有什么方法可以不使用线路找零

row = m[-1]

再一次?

最佳答案

您想要实现的目标目前在 numpy 库的范围内是不可能的。请参阅 numpy arrays occupying a contiguous block of memory 上的此答案.

您可以使用 rx 库通过将最后一行设为主题来解决此问题。

import numpy as np
from rx import Observable, Observer
from rx.subjects import Subject

m = np.zeros((5,5))

m_stream = Subject()
last_row_stream = Subject()
last_row = None

def update_last(v):
global last_row
last_row = v

last_row_stream.subscribe(on_next=update_last)
last_row_stream.on_next(m[-1])

print(last_row)

m_stream.map(
lambda v: np.insert(v, 2, [0, 0, 1, 1, 1], axis=1)
).subscribe(lambda v: last_row_stream.on_next(v[-1]))

m_stream.on_next(m)


print(last_row)

关于python - 在 numpy 数组上查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45779187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com