0

for ループ内に可変サイズの 1 次元配列があり、これらの配列をサイズ 40x4000 の 2 次元配列の行に配置しています。最終的なサイズが 1x4000 になり、2 次元配列に収まるように、1 次元配列の最後にゼロを追加するにはどうすればよいですか?

例えば。

size of 1-D: 3700 , so add 300 zeros at the end
size of 1-D: 3800 , so add 200 zeros at the end

for i in range(n)
   s = func(i) #returns row vector
   # what to do here?
   two_dim[i] = s

編集:これらはnumpy配列であり、リストではありません

4

1 に答える 1

1

より小さな入力に対してこれを行うと、うまくいけば、これがあなたが探しているものです。

初期リストの場合testList = [9, 9, 9, 8, 4, 5]

>>> maxListLen = 10
>>> testList = testList+[0]*(maxListLen-len(testList))
[9, 9, 9, 8, 4, 5, 0, 0, 0, 0]

numpyOPは配列を探しているので。

>>> testArray = numpy.array([1, 2, 3, 4])
>>> testArray = numpy.append(testArray, [0]*(maxListLen-len(testArray)))
>>> testArray
array([1, 2, 3, 4, 0, 0, 0, 0, 0, 0])
于 2013-05-29T21:21:37.167 に答える