我如何重塑这个numpy数组

问题描述 投票:0回答:3

我有这样的数组:

[[[a, b], [c, d], [e, f]]]

当我对这个数组进行整形时,它会给出(2,)

我尝试了reshape(-1)方法,但是没有用。我想将此数组重塑为:

[[a, b], [c, d], [e, f]]

我该如何转换?如果您有帮助,我会很高兴。

python arrays list reshape
3个回答
0
投票
chars_list = [[["a", "b"], ["c", "d"], ["e", "f"]]]
chars_list_one = []
for element in chars_list:
    for element_one in element:
        chars_list_one.append(element_one)
print(chars_list_one)

3
投票

您可以使用numpy.squeeze功能。

numpy.squeeze

输出:

a = np.array([[["a", "b"], ["c", "d"], ["e", "f"]]])
print(a.shape)
print(a)
(1, 3, 2)
[[['a' 'b']
  ['c' 'd']
  ['e' 'f']]]

输出:

b = a.squeeze(0)
print(b.shape)
print(b)

0
投票

您可以使用(3, 2) [['a' 'b'] ['c' 'd'] ['e' 'f']] 方法。

.squeeze
© www.soinside.com 2019 - 2024. All rights reserved.