我有一个数组数组,就像这样:
array([[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype=int64)],
[array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype=int64)],
[array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
17616, 25146, 32645, 41276], dtype=int64)],
...,
[array([24534, 8230, 14267, 9352, 3543, 29397, 900, 32398, 34262,
37646, 11930, 37173], dtype=int64)],
[array([25157], dtype=int64)],
[array([ 8859, 20850, 19322, 8075], dtype=int64)]], dtype=object)
我想要的是
array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668,33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665,46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,17616, 25146, 32645, 41276
...,
24534, 8230, 14267, 9352, 3543, 29397, 900, 32398, 34262,
37646, 11930, 37173,25157 8859, 20850, 19322, 8075, dtype=object)
我已经搜索了一些解决方案,但是似乎所有这些都用于np.array或list,而不适用于array。
functools.reduce(operator.iconcat, orders2.values.tolist(), [])
[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype=int64),
array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype=int64),
array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
17616, 25146, 32645, 41276], dtype=int64),...
orders2.values.flatten()
array([array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype=int64),
array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype=int64),
我什至无法将数组转换为列表
[sub.tolist() for sub in orders2.values]
[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype=int64),
array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype=int64),
array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
17616, 25146, 32645, 41276], dtype=int64),...
orders2.values.flatten()
array([array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype=int64),
array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype=int64),...
我发现很难获得有关数组类的一些信息,所有内容都是列表或np.array
答案 0 :(得分:4)
使用列表推导,然后转换回array
:
>>> arr = array([[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype='int64')],
[array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype='int64')],
[array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
17616, 25146, 32645, 41276], dtype='int64')],
[array([24534, 8230, 14267, 9352, 3543, 29397, 900, 32398, 34262,
37646, 11930, 37173], dtype='int64')],
[array([25157], dtype='int64')],
[array([ 8859, 20850, 19322, 8075], dtype='int64')]], dtype=object)
>>> array([x for i in arr.tolist() for x in i[0].tolist()])
array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668,
33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665, 46842,
26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285, 17616,
25146, 32645, 41276, 24534, 8230, 14267, 9352, 3543, 29397,
900, 32398, 34262, 37646, 11930, 37173, 25157, 8859, 20850,
19322, 8075])
>>>
答案 1 :(得分:1)
In [141]: array=np.array;
...: arr = array([[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
...: dtype='int64')],
...: [array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
...: dtype='int64')],
...: [array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
...: 17616, 25146, 32645, 41276], dtype='int64')],
...: [array([24534, 8230, 14267, 9352, 3543, 29397, 900, 32398, 34262,
...: 37646, 11930, 37173], dtype='int64')],
...: [array([25157], dtype='int64')],
...: [array([ 8859, 20850, 19322, 8075], dtype='int64')]], dtype=object)
In [142]: np.concatenate(arr.ravel())
Out[142]:
array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668,
33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665, 46842,
26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285, 17616,
25146, 32645, 41276, 24534, 8230, 14267, 9352, 3543, 29397,
900, 32398, 34262, 37646, 11930, 37173, 25157, 8859, 20850,
19322, 8075])
形状为2d:
In [143]: arr.shape
Out[143]: (6, 1)
arr.ravel()
使其成为1d(6,),np.concatenate
加入数组列表(或可迭代)。