利用numpy实现list降维

python读取数据库得到的事一个类似二维数组的list,有时候需要降维操作,numpy提供一个很有用的函数,可以直接使用

import numpy as np
a = np.array([[1, 2], [3, 4], [9, 8]]) #设置源数据
b = a.flatten()#降维 得到一个numpy.ndarray类型的数据
c = list(b)#转换数据类型
print(b)
print(c)
print('type of "b":',end="  ")
print(type(b))
print('type of "c":',end="  ")
print(type(c))

得到的结果如下

[1 2 3 4 9 8]
[1, 2, 3, 4, 9, 8]
type of "b":  <class 'numpy.ndarray'>
type of "c":  <class 'list'>
人在中年,一事无成,瞎学
原文地址:https://www.cnblogs.com/jilingxf/p/15582075.html