如何重塑这个numpy数组

2024-06-01 13:06:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的排列:

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

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

我尝试了reshape(-1)方法,但没有成功。我想把这个数组改成:

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

我怎样才能转换它?如果你能帮忙,我会很高兴的。你知道吗


Tags: 方法数组我会reshape
3条回答

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

您可以使用^{}函数。你知道吗

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']]

相关问题 更多 >