我如何重塑这个 numpy 数组

How can i reshape this numpy array

我有这样的数组:

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

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

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

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

我如何转换它?如果你能帮上忙,我会很高兴。

您可以使用.squeeze方法。

import numpy as np

a = np.array([[['a', 'b'], ['c', 'd'], ['e', 'f']]])
a.squeeze()

输出:

array([['a', 'b'],
       ['c', 'd'],
       ['e', 'f']], dtype='<U1')

您可以使用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)

输出:

(3, 2)
[['a' 'b']
 ['c' 'd']
 ['e' 'f']]
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)