这是我的数组,我尝试了一些切片操作
但它不起作用
有人能告诉我怎么做吗?
类型为numpy.ndarray
x=[[10 34]
[34 45]
[12 12]
[12 34]
[23 23]]我想得到一个类似于这个-的输出:
x=[10 34
34 45
12 12
12 34
23 23]如果
发布于 2020-07-20 11:46:19
你可以这样做:
import numpy as np
x=[[10, 34],
[34, 45],
[12, 12],
[12, 34],
[23, 23]]没有numpy的First:
flatten_x = [item for sublist in x for item in sublist]具有扁平的第二:
flatten_x = np.array(x).flatten().tolist()使用ravel的第三代,在所有这些中速度更快:
flatten_x = np.array(x).ravel()整形的第四:
flatten_x = np.array(x).reshape(-1)输出:
print(flatten_x)发布于 2020-07-20 11:28:41
您可以尝试x.reshape(-1),如果要将其转换为list,则可以使用list(x.reshape(-1))
发布于 2020-07-20 11:33:23
可以简单地使用numpy函数ravel作为
a = np.array([[1,2,3], [4,5,6]])
a.ravel()结果是:数组(1,2,3,4,5,6)
https://stackoverflow.com/questions/62994341
复制相似问题