你可以试试这个:
>>> import numpy as np
>>> class A:
... def __init__(self, values):
... self.partposit = values
...
>>> PARTS = dict((index, A(np.zeros((50000, 12)))) for index in xrange(163))
>>> p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))
>>> p1.shape
(50000, 12, 163)
>>>将它堆叠在我的机器上花了几秒钟。
>>> import timeit
>>> timeit.Timer('p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))', "from __main__ import np, PARTS").timeit(number = 1)
2.1245520114898682numpy.dstack接受一系列数组并将它们堆叠在一起,因为如果我们只是给它列表而不是自己连续堆叠它们会更快。
numpy.dstack(tup)
Stack arrays in sequence depth wise (along third axis).
Takes a sequence of arrays and stack them along the third axis to make a single array