0

here is my array, i tried some slicing operation

But it did not work

can someone tell me how to do that?

type is numpy.ndarray


x=[[10 34]
   [34 45]
   [12 12]
   [12 34]
   [23 23]]

I want to get a output like this -:

x=[10 34
   34 45
   12 12
   12 34
   23 23]

if

Jose
  • 330
  • 1
  • 3
  • 13

3 Answers3

1

You can do by:

    import numpy as np
x=[[10, 34],
   [34, 45],
   [12, 12],
   [12, 34],
   [23, 23]]
   

First without numpy:

flatten_x = [item for sublist in x for item in sublist]

Second with flatten:

flatten_x = np.array(x).flatten().tolist()

Third with ravel which is faster among all:

flatten_x = np.array(x).ravel()

Fourth with reshape:

flatten_x = np.array(x).reshape(-1)

Output:

print(flatten_x)
Ashish Karn
  • 1,127
  • 1
  • 9
  • 20
0

You can try x.reshape(-1) and if you want to convert it to list then you can use list(x.reshape(-1))

Rajat Agarwal
  • 174
  • 3
  • 6
-1

You can simply use the numpy function ravel as

a = np.array([[1,2,3], [4,5,6]])
a.ravel()

Result would be: array([1, 2, 3, 4, 5, 6])