将1D阵列倒入Python的多维字段



亲爱的,

我想在python中做以下操作,但没有发现有关如何从 data 中生成 data_rejuggled (MATLAB除外)的参考:

from collections import namedtuple
data=[1,2,2,2,3,4,5,5,5,5]
dims=[1,3,1,1,4]
data_rejuggled=somefunction(data,dims) #<--THIS is what I want to know
print data_rejuggled
  [1,[2,2,2],3,4,[5,5,5,5]] 
names=["a","b","c","d","e"] 
recID ='r'+str(1) #define a rec id
s = namedtuple(recID, names, verbose=False) #create tuple
s=s(*data_rejuggled) #fill tuple
print s
  r1(a=1, b=[2, 2, 2], c=3, d=4, e=[5, 5, 5, 5])

背景:

  • 数据是从二进制文件解码
  • 数据可能非常大

谢谢您的阅读,

f#

以防万一有人打扰,这就是我最终解决的方式:

def somefunction(data,dims):
  ls2=np.add.accumulate(np.array(dims)) #gets indices for last members of each field
  ls=np.roll(ls2,1)                     #gets indices for first members of each field
  ls[0]=0                               #set index of first field to zero
  return [data[i:j] for i,j in zip(ls,ls2)] #return members of of each field as arrays

相关内容

  • 没有找到相关文章

最新更新