numpy:从 ndarray 创建双端队列,这是一个数组数组
numpy: create deque from ndarray which is an array of arrays
我有一个这种形式的 numpy ndarray:
inputs = np.array([[1],[2],[3]])
如何将此 ndarray
转换为 deque
(collections.deque
) 以便保留结构(数组的数组)并且我可以应用正常的双端队列方法,例如 popleft()
和 append()
?例如:
inputs.popleft()
->>> [[2],[3]]
inputs.append([4])
->>> [[2],[3], [4]]
嗯,我认为你可以做到:
inputs = np.array([[1],[2],[3]])
inputs = collections.deque([list(i) for i in inputs])
inputs.append([4])
inputs.popleft()
编辑。
我编辑了代码
我认为你可以将 inputs
直接传递给 deque
from collections import deque
i = deque(inputs)
In [1050]: i
Out[1050]: deque([array([1]), array([2]), array([3])])
In [1051]: i.popleft()
Out[1051]: array([1])
In [1052]: i
Out[1052]: deque([array([2]), array([3])])
In [1053]: i.append([4])
In [1054]: i
Out[1054]: deque([array([2]), array([3]), [4]])
稍后,当您想要 numpy.array
返回时,只需将 deque 传回 numpy
np.array(i)
Out[1062]:
array([[2],
[3],
[4]])
我有一个这种形式的 numpy ndarray:
inputs = np.array([[1],[2],[3]])
如何将此 ndarray
转换为 deque
(collections.deque
) 以便保留结构(数组的数组)并且我可以应用正常的双端队列方法,例如 popleft()
和 append()
?例如:
inputs.popleft()
->>> [[2],[3]]
inputs.append([4])
->>> [[2],[3], [4]]
嗯,我认为你可以做到:
inputs = np.array([[1],[2],[3]])
inputs = collections.deque([list(i) for i in inputs])
inputs.append([4])
inputs.popleft()
编辑。 我编辑了代码
我认为你可以将 inputs
直接传递给 deque
from collections import deque
i = deque(inputs)
In [1050]: i
Out[1050]: deque([array([1]), array([2]), array([3])])
In [1051]: i.popleft()
Out[1051]: array([1])
In [1052]: i
Out[1052]: deque([array([2]), array([3])])
In [1053]: i.append([4])
In [1054]: i
Out[1054]: deque([array([2]), array([3]), [4]])
稍后,当您想要 numpy.array
返回时,只需将 deque 传回 numpy
np.array(i)
Out[1062]:
array([[2],
[3],
[4]])