python 中的list和array的不同之处及转换问题


Posted in Python onMarch 13, 2018

python中的list是python的内置数据类型,list中的数据类不必相同的,而array的中的类型必须全部相同。在list中的数据类型保存的是数据的存放的地址,简单的说就是指针,并非数据,这样保存一个list就太麻烦了,例如list1=[1,2,3,'a']需要4个指针和四个数据,增加了存储和消耗cpu。

      numpy中封装的array有很强大的功能,里面存放的都是相同的数据类型

list1=[1,2,3,'a'] 
print list1 
a=np.array([1,2,3,4,5]) 
b=np.array([[1,2,3],[4,5,6]]) 
c=list(a)  # array到list的转换 
print a,np.shape(a) 
print b,np.shape(b) 
print c,np.shape(c)

运行结果:

[1, 2, 3, 'a'] # 元素数据类型不同,并且用逗号隔开 
[1 2 3 4 5] (5L,) # 一维数组,类型用tuple表示 
[[1 2 3] 
 [4 5 6]] (2L, 3L) 
[1, 2, 3, 4, 5] (5L,)

创建:

    array的创建:参数既可以是list,也可以是元组.使用对应的属性shape直接得到形状

a=np.array((1,2,3,4,5))# 参数是元组 
b=np.array([6,7,8,9,0])# 参数是list 
c=np.array([[1,2,3],[4,5,6]])# 参数二维数组 
print a,b, 
c.shape()

   也可以直接改变属性array的形状,-1代表的是自己推算。这里并不是T, reshape(())也可以

c = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]]) 
c.shape # (3L, 4L) 
c.shape=4,-1  //c.reshape((2,-1)) 
c  
<pre style="box-sizing: border-box; overflow: auto; font-size: 14px; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 17.0001px; word-break: break-all; word-wrap: break-word; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline; background-color: rgb(255, 255, 255);">array([[ 1, 2, 3], 
    [ 4, 4, 5], 
    [ 6, 7, 7], 
    [ 8, 9, 10]])

 
   这里的reshape最终相当于是一个浅拷贝,也就是说还是和原来的书c使用相同的内存空间

d=c.reshape((2,-1)) 
d[1:2]=100 
c 
array([[ 1,  2,  3],
    [ 4,  4,  5],
    [100, 100, 100],
    [100, 100, 100]])

   前面在创建数组的时候并没有使用数据类型,这里我们也可以使用数据类型。默认的是int32.

a1=np.array([[1,2,3],[4,5,6]],dtype=np.float64) 
print a1.dtype,a.dtype #float64 int32<pre style="margin-top: 0px; margin-bottom: 0px; line-height: 17.0001px; box-sizing: border-box; overflow: auto; font-size: 14px; padding: 0px; word-break: break-all; word-wrap: break-word; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline; background-color: rgb(255, 255, 255);">

前面在创建的时候我们都是使用的np.array()方法从tuple或者list转换成为array,感觉很是费劲,numpy自己提供了很多的方法让我们自己直接创建一个array.

arr1=np.arange(1,10,1) #  
arr2=np.linspace(1,10,10) 
print arr1,arr1.dtype 
print arr2,arr2.dtype 
[1 2 3 4 5 6 7 8 9] int32
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.] float64

   np.arange(a,b,c)表示产生从a-b不包括b,间隔为c的一个array,数据类型默认是int32。但是linspace(a,b,c)表示的是把a-b平均分成c分,它包括b。   

   有时候我们需要对于每一个元素的坐标进行赋予不同的数值,可以使用fromfunction函数

def fun(i): 
  return i%4+2 
np.fromfunction(fun,(10,)) 
array([ 2., 3., 4., 5., 2., 3., 4., 5., 2., 3.])

   fromfunction必须支持多维数组,所以他的第二个参数必须是一个tuple,只能是(10,),(10)是错误的。

def fun2(i,j): 
  return (i+1)*(j+1) 
np.fromfunction(fun2,(9,9)) 

array([[ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
    [ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18.],
    [ 3.,  6.,  9., 12., 15., 18., 21., 24., 27.],
    [ 4.,  8., 12., 16., 20., 24., 28., 32., 36.],
    [ 5., 10., 15., 20., 25., 30., 35., 40., 45.],
    [ 6., 12., 18., 24., 30., 36., 42., 48., 54.],
    [ 7., 14., 21., 28., 35., 42., 49., 56., 63.],
    [ 8., 16., 24., 32., 40., 48., 56., 64., 72.],
    [ 9., 18., 27., 36., 45., 54., 63., 72., 81.]])

        虽然说,这里提供了很多的直接产生array的方式,但是大部分情况我们都是会从list进行转换,因为在实际的处理中,我们需要从txt加载文件,那样直接读入的数据显示存放到list中,需要处理的时候我们转换到array,因为
array的设计更加符合我们的使用,涉及到矩阵的运算在使用mat,那么list主要就是用进行元素的索取。

def loaddataSet(fileName):  
  file=open(fileName)  
  dataMat=[] // 
  for line in file.readlines():  
    curLine=line.strip().split('\t')  
    floatLine=map(float,curLine)//这里使用的是map函数直接把数据转化成为float类型  
    dataMat.append(floatLine)  
  return dataMat

    上面的韩顺返回最终的数据就是最初的list数据集,再根据不同的处理需求是转化到array还是mat。其实array是mat的父类,能用mat的地方,array理论上都能传入。

 元素访问:    

arr[5] #5 
arr[3:5] #array([3, 4]) 
arr[:5] #array([0, 1, 2, 3, 4]) 
arr[:-1]# array([0, 1, 2, 3, 4, 5, 6, 7, 8]) 
arr[:] #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 
arr[2:4]=100 # array([ 0,  1, 100, 100,  4,  5,  6,  7,  8,  9]) 
arr[1:-1:2] #array([ 1, 100,  5,  7]) 2 是间隔 
arr[::-1] #array([ 9,  8,  7,  6,  5,  4, 100, 100,  1,  0])  
arr[5:2:-1]# -1的间隔表示从右向左所以5>2 #array([ 5,  4, 100])

   上面是array的一维数组的访问方式,我们再来看看二维的处理方式

print c[1:2]# c[1:2].shape-->(1L, 3L) 
print c[1:2][0] # shape-->(3L,) 
[[4 4 5]]
[4 4 5]
[python] view plain copy 
print c[1] 
print c[1:2] 
[4 4 5]
[[4 4 5]]
[python] view plain copy 
print c[1][2] 
print c[1:4] 
print c[1:4][0][2] 
5
[[ 4  4  5]
 [100 100 100]
 [100 100 100]]
5

   可以看出对于有:的表达最终的结果外面还嵌套一层list的[],。访问的一定要注意,python最bug的就是,语法
灵活,不管怎样写索引语法都是正确的,但是最终的书结果却让你大跌眼镜。

    还有array的索引最终产生的是一个一个原始数据的浅拷贝,还和原来的数据共用一块儿内存

b=arr[1:6] 
b[:3]=0 
arr #<pre style="box-sizing: border-box; overflow: auto; font-size: 14px; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 17.0001px; word-break: break-all; word-wrap: break-word; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline; background-color: rgb(255, 255, 255);">array([0, 0, 0, 0, 4, 5, 6, 7, 8, 9])

    产生上面的原因是因为array中直接存放的数据,拷贝的话直接拿走的是pointer,没有取走数据,但是list却会直接发生深拷贝,数据指针全部带走

list1=list(c) 
list1[1]=0 
list1 #上面修改的0并没有被改变 
[array([1, 2, 3]), 0, array([100, 100, 100]), array([100, 100, 100])]

   除了这些之外还有自己的更加牛掰的方式(只能用array)

   1)使用布尔数组.感觉甚是强大,就不要自己写什么判断语句啦,注意这种方式得到结果不和原始数组共享空间。布尔索引仅仅适用于数组array,list没资格用。布尔索引最终得到下标索引为true的数据。索引只能是布尔数组

a=np.array(a*2) 
a>5 
a[a>5] #  
array([16, 32, 48, 64, 80, 16, 32, 48, 64, 80])

   2)列表索引

      列表索引可以是数组和list。返回的数据不和原来的数据共享内存。索引可以是list和array

x=np.arange(10) 
index=[1,2,3,4,5] 
arr_index=np.array(index) 
print x 
print x[index] # list索引 
print x[arr_index] # array索引 
[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5]
[1 2 3 4 5]

  array和list区别*2

a=np.arange(10) 
lista=list(a) 
print a*2 
print lista*2 
[ 0 2 4 6 8 10 12 14 16 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  array的广播

a = np.arange(0, 60, 10).reshape(-1, 1) 
b = np.arange(0, 5) 
print a 
print b 
[[ 0]
 [10]
 [20]
 [30]
 [40]
 [50]]
[0 1 2 3 4]
print np.add(a,b,c) 
[[ 0 1 2 3 4]
 [10 11 12 13 14]
 [20 21 22 23 24]
 [30 31 32 33 34]
 [40 41 42 43 44]
 [50 51 52 53 54]]

总结

以上所述是小编给大家介绍的python 中的list和array的不同之处及转换问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python解析文件示例
Jan 23 Python
Python Requests 基础入门
Apr 07 Python
Python 使用SMTP发送邮件的代码小结
Sep 21 Python
python决策树之C4.5算法详解
Dec 20 Python
python assert的用处示例详解
Apr 01 Python
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
Jul 01 Python
Python实现的爬取豆瓣电影信息功能案例
Sep 15 Python
python基于socket实现的UDP及TCP通讯功能示例
Nov 01 Python
python实现五子棋游戏(pygame版)
Jan 19 Python
Python 中的pygame安装与配置教程详解
Feb 10 Python
python实现发送QQ邮件(可加附件)
Dec 23 Python
Django一小时写出账号密码管理系统
Apr 29 Python
python2.7安装图文教程
Mar 13 #Python
Python cookbook(数据结构与算法)对切片命名清除索引的方法
Mar 13 #Python
Django使用Celery异步任务队列的使用
Mar 13 #Python
特征脸(Eigenface)理论基础之PCA主成分分析法
Mar 13 #Python
python Celery定时任务的示例
Mar 13 #Python
人脸识别经典算法一 特征脸方法(Eigenface)
Mar 13 #Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
Mar 13 #Python
You might like
php基础知识:控制结构
2006/12/13 PHP
php中3des加密代码(完全与.net中的兼容)
2012/08/02 PHP
php接口和抽象类使用示例详解
2014/03/02 PHP
JQuery基础语法小结
2015/02/27 Javascript
JavaScript实现鼠标点击后层展开效果的方法
2015/05/13 Javascript
NodeJS遍历文件生产文件列表功能示例
2017/01/22 NodeJs
js求数组中全部数字可拼接出的最大整数示例代码
2017/08/25 Javascript
Vue实现textarea固定输入行数与添加下划线样式的思路详解
2018/06/28 Javascript
JS返回页面时自动回滚到历史浏览位置
2018/09/26 Javascript
Iview Table组件中各种组件扩展的使用
2018/10/20 Javascript
JavaScript交换两个变量方法实例
2019/11/25 Javascript
jQuery开发仿QQ版音乐播放器
2020/07/10 jQuery
微信小程序实现modal弹出框遮罩层组件(可带文本框)
2020/12/20 Javascript
Python实现的RSS阅读器实例
2015/07/25 Python
python基础教程之分支、循环简单用法
2016/06/16 Python
Windows下Python2与Python3两个版本共存的方法详解
2017/02/12 Python
python中的文件打开与关闭操作命令介绍
2018/04/26 Python
python实现汉诺塔算法
2021/03/01 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2019/01/19 Python
Python 利用切片从列表中取出一部分使用的方法
2019/02/01 Python
python re.sub()替换正则的匹配内容方法
2019/07/22 Python
详解python 降级到3.6终极解决方案
2020/02/06 Python
解决jupyter notebook 出现In[*]的问题
2020/04/13 Python
pytorch中 gpu与gpu、gpu与cpu 在load时相互转化操作
2020/05/25 Python
HTML5 本地存储之如果没有数据库究竟会怎样
2013/04/25 HTML / CSS
DERMAdoctor官网:美国著名皮肤护理品牌
2019/07/06 全球购物
Pamela Love官网:纽约设计师Pamela Love的精美、时尚和穿孔珠宝
2020/10/19 全球购物
PyQt QMainWindow的使用示例
2021/03/24 Python
干部下基层实施方案
2014/03/14 职场文书
奥巴马开学演讲稿
2014/05/15 职场文书
初中英语教师个人工作总结
2015/02/09 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
《称赞》教学反思
2016/02/17 职场文书
励志正能量20句:送给所有为梦想拼搏的人
2019/11/11 职场文书
css filter和getUserMedia的联合使用
2022/02/24 HTML / CSS
vue.js 使用原生js实现轮播图
2022/04/26 Vue.js