首页 > 编程语言 > 详细

pyecharts 学习笔记(6)--numpy列表与数组的相互转换

时间:2020-06-02 22:29:24      阅读:76      评论:0      收藏:0      [点我收藏+]
由于pyecharts的数据格式不接受numpy的一维数组的格式,所以需要转换


列表转数组
1、转成数组的matrix对象,使用np.mat()方法。

In [1]: a = [[2,3,4],[4,7,1]]                                                                                                                                                                 
 
In [2]: np.mat(a)                                                                                                                                                                             
Out[2]: 
matrix([[2, 3, 4],
        [4, 7, 1]])
2、转成数组的ndarray对象,使用np.array()方法。

In [1]: a = [[2,3,4],[4,7,1]]  
In [2]: np.array(a)                                                                                                                                                                           
Out[2]: 
array([[2, 3, 4],
       [4, 7, 1]])
数组转列表
不管是matrix对象还是ndarray对象,都可以使用object.tolist()方法转为成列表。

In [1]: a = np.mat(((1,2),(2,3)))                                                                                                                                                             
 
In [2]: a.tolist()                                                                                                                                                                            
Out[2]: [[1, 2], [2, 3]]
 
In [3]: b = np.array(((1,2),(2,3)))                                                                                                                                                           
 
In [4]: b.tolist()                                                                                                                                                                            
Out[4]: [[1, 2], [2, 3]]
 

 

numpy列表与数组的相互转换

  • 列表转数组

1、转成数组的matrix对象,使用np.mat()方法。

  1.  
    In [1]: a = [[2,3,4],[4,7,1]]
  2.  
     
  3.  
    In [2]: np.mat(a)
  4.  
    Out[2]:
  5.  
    matrix([[2, 3, 4],
  6.  
    [4, 7, 1]])

2、转成数组的ndarray对象,使用np.array()方法。

  1.  
    In [1]: a = [[2,3,4],[4,7,1]]
  2.  
    In [2]: np.array(a)
  3.  
    Out[2]:
  4.  
    array([[2, 3, 4],
  5.  
    [4, 7, 1]])
  • 数组转列表

不管是matrix对象还是ndarray对象,都可以使用object.tolist()方法转为成列表。

  1.  
    In [1]: a = np.mat(((1,2),(2,3)))
  2.  
     
  3.  
    In [2]: a.tolist()
  4.  
    Out[2]: [[1, 2], [2, 3]]
  5.  
     
  6.  
    In [3]: b = np.array(((1,2),(2,3)))
  7.  
     
  8.  
    In [4]: b.tolist()
  9.  
    Out[4]: [[1, 2], [2, 3]]

 

pyecharts 学习笔记(6)--numpy列表与数组的相互转换

原文:https://www.cnblogs.com/vincent-sh/p/13033898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!