0

亲爱的,

我想在 python 中执行以下操作,但没有找到关于如何从数据生成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#

4

1 回答 1

0

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

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
于 2017-12-05T10:22:21.890 回答