Anil
Anil

Reputation: 31

How can i reshape this numpy array

I have and array like this :

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

When i shape to this array it gives (2,)

I tried reshape(-1) method but i did not work. I want to reshape this array into:

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

How can i convert that? I would be glad if you help.

Upvotes: 0

Views: 98

Answers (3)

James
James

Reputation: 36756

You can use the .squeeze method.

import numpy as np

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

Output:

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

Upvotes: 0

uniQ
uniQ

Reputation: 125

You can use the numpy.squeeze function.

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

Output:

(1, 3, 2)
[[['a' 'b']
  ['c' 'd']
  ['e' 'f']]]
b = a.squeeze(0)
print(b.shape)
print(b)

Output:

(3, 2)
[['a' 'b']
 ['c' 'd']
 ['e' 'f']]

Upvotes: 3

Shainidze David
Shainidze David

Reputation: 36

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)

Upvotes: 0

Related Questions