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
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)
You can try x.reshape(-1)
and if you want to convert it to list then you can use list(x.reshape(-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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.