gpt4 book ai didi

Python动态数组分配,Matlab风格

转载 作者:行者123 更新时间:2023-11-28 21:27:12 26 4
gpt4 key购买 nike

我正在尝试将我构建的一些 Matlab 库移动到 python 环境中。到目前为止,我面临的最大问题是基于索引规范的数组动态分配。例如,使用 Matlab,键入以下内容:

x = [1 2];
x(5) = 3;

会导致:

x = [ 1     2     0     0     3]

换句话说,我事先并不知道 (x) 的大小,也不知道它的内容。该数组必须根据我提供的索引动态定义。

在 python 中,尝试以下操作:

from numpy import *
x = array([1,2])
x[4] = 3

会导致以下错误:IndexError:索引超出范围。解决方法是在循环中递增数组,然后将所需的值分配为:

from numpy import *
x = array([1,2])

idx = 4
for i in range(size(x),idx+1):
x = append(x,0)

x[idx] = 3
print x

它可以工作,但它不是很方便,而且对于 n 维数组来说可能会变得非常麻烦。我想通过子类化 ndarray 来实现我的目标,但我不确定它是否可行。有人知道更好的方法吗?


感谢您的快速回复。我不知道 setitem 方法(我是 Python 的新手)。我简单地覆盖了 ndarray 类,如下所示:

import numpy as np

class marray(np.ndarray):

def __setitem__(self, key, value):

# Array properties
nDim = np.ndim(self)
dims = list(np.shape(self))

# Requested Index
if type(key)==int: key=key,
nDim_rq = len(key)
dims_rq = list(key)

for i in range(nDim_rq): dims_rq[i]+=1

# Provided indices match current array number of dimensions
if nDim_rq==nDim:

# Define new dimensions
newdims = []
for iDim in range(nDim):
v = max([dims[iDim],dims_rq[iDim]])
newdims.append(v)

# Resize if necessary
if newdims != dims:
self.resize(newdims,refcheck=False)

return super(marray, self).__setitem__(key, value)

它就像一个魅力!但是,我需要修改上面的代码,以便 setitem 允许更改此请求后的维数:

a = marray([0,0])
a[3,1,0] = 0

不幸的是,当我尝试使用诸如

之类的 numpy 函数时
self = np.expand_dims(self,2)

返回的类型是 numpy.ndarray 而不是 ma​​in.marray。如果提供 marray 作为输入,关于如何强制 numpy 函数输出 marray 的任何想法?我认为使用 array_wrap 应该是可行的,但我一直找不到确切的方法。任何帮助将不胜感激。

最佳答案

冒昧地更新我来自 Dynamic list that automatically expands 的旧答案.认为这应该可以满足您的大部分需求

class matlab_list(list):
def __init__(self):
def zero():
while 1:
yield 0
self._num_gen = zero()

def __setitem__(self,index,value):
if isinstance(index, int):
self.expandfor(index)
return super(dynamic_list,self).__setitem__(index,value)

elif isinstance(index, slice):
if index.stop<index.start:
return super(dynamic_list,self).__setitem__(index,value)
else:
self.expandfor(index.stop if abs(index.stop)>abs(index.start) else index.start)
return super(dynamic_list,self).__setitem__(index,value)

def expandfor(self,index):
rng = []
if abs(index)>len(self)-1:
if index<0:
rng = xrange(abs(index)-len(self))
for i in rng:
self.insert(0,self_num_gen.next())
else:
rng = xrange(abs(index)-len(self)+1)
for i in rng:
self.append(self._num_gen.next())

# Usage
spec_list = matlab_list()
spec_list[5] = 14

关于Python动态数组分配,Matlab风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472333/

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