我有一个像numpy这样的对象ndarray:
$xl = New-Object -comobject Excel.Application
$xl.visible = $true
$xl.DisplayAlerts = $false
$xl.sheetsInNewWorkbook = $batches
$xl.workbooks.add()
for($i=1;$i -lt $batches;$i++){
$ws1 = $ws.sheets.item($i)
$ws1.activate()
$ws1.name = "Batch$i"
}
是否有像array([array([1, 2]), array([2]), array([3]), array([4, 2, 3]),
array([5, 4]), array([6, 3])], dtype=object)
这样的方法来展平这个数组? e.g:
np.ravel()
答案 0 :(得分:2)
您可以使用np.concatenate
转换为“扁平化”的numpy数组,如下所示 -
In [355]: obj1 = np.array([np.array([1, 2]), np.array([2]), np.array([3]), np.array([4, 2, 3]),
...: np.array([5, 4]), np.array([6, 3])], dtype=object)
In [356]: np.concatenate(obj1)
Out[356]: array([1, 2, 2, 3, 4, 2, 3, 5, 4, 6, 3])
然后,您可以转换为包含.tolist()
-
In [357]: np.concatenate(obj1).tolist()
Out[357]: [1, 2, 2, 3, 4, 2, 3, 5, 4, 6, 3]
答案 1 :(得分:1)
您可以使用numpy.concatenate
,顾名思义,它将数组的列表/数组连接成一个展平的numpy
- 数组:
import numpy as np
from numpy import array
# some test data
a = array([array([1, 2]), array([2]), array([3]), array([4, 2, 3]),
array([5, 4]), array([6, 3])], dtype=object)
np.concatenate(a)
# array([1, 2, 2, 3, 4, 2, 3, 5, 4, 6, 3])