跟老齐学Python之list和str比较


Posted in Python onSeptember 20, 2014

相同点

都属于序列类型的数据

所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数减1结束。

例如:

>>> welcome_str = "Welcome you"
>>> welcome_str[0]
'W'
>>> welcome_str[1]
'e'
>>> welcome_str[len(welcome_str)-1]
'u'
>>> welcome_str[:4]
'Welc'
>>> a = "python"
>>> a*3
'pythonpythonpython'

>>> git_list = ["qiwsir","github","io"]
>>> git_list[0]
'qiwsir'
>>> git_list[len(git_list)-1]
'io'
>>> git_list[0:2]
['qiwsir', 'github']
>>> b = ['qiwsir']
>>> b*7
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']

对于此类数据,下面一些操作是类似的:

>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str  #用+号连接str
'hello,world,Welcome you'
>>> welcome_str       #原来的str没有受到影响,即上面的+号连接后从新生成了一个字符串
'Welcome you'
>>> first
'hello,world'

>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list   #用+号连接list,得到一个新的list
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['python']

>>> len(welcome_str)  #得到字符数
11
>>> len(git_list)    #得到元素数
3

区别

list和str的最大区别是:list是原处可以改变的,str则原处不可变。这个怎么理解呢?

首先看对list的这些操作,其特点是在原处将list进行了修改:

>>> git_list
['qiwsir', 'github', 'io']

>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']

>>> git_list[1]        
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']

>>> git_list.insert(1,"algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']

>>> git_list.pop()
'python'

>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']

以上这些操作,如果用在str上,都会报错,比如:

>>> welcome_str
'Welcome you'

>>> welcome_str[1] = 'E'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

>>> del welcome_str[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object doesn't support item deletion

>>> welcome_str.append("E")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

如果要修改一个str,不得不这样。

>>> welcome_str
'Welcome you'
>>> welcome_str[0] + "E" + welcome_str[2:] #从新生成一个str
'WElcome you'
>>> welcome_str             #对原来的没有任何影响
'Welcome you'

其实,在这种做法中,相当于从新生成了一个str。

多维list

这个也应该算是两者的区别了,虽然有点牵强。在str中,里面的每个元素只能是字符,在list中,元素可以是任何类型的数据。前面见的多是数字或者字符,其实还可以这样:

>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix[0][1]
2
>>> mult = [[1,2,3],['a','b','c'],'d','e']
>>> mult
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
>>> mult[1][1]
'b'
>>> mult[2]
'd'

以上显示了多维list以及访问方式。在多维的情况下,里面的list也跟一个前面元素一样对待。

list和str转化

str.split()

这个内置函数实现的是将str转化为list。其中str=""是分隔符。

在看例子之前,请看官在交互模式下做如下操作:

>>>help(str.split)
得到了对这个内置函数的完整说明。特别强调:这是一种非常好的学习方法

split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

不管是否看懂上面这段话,都可以看例子。还是希望看官能够理解上面的内容。

>>> line = "Hello.I am qiwsir.Welcome you." 

>>> line.split(".")   #以英文的句点为分隔符,得到list
['Hello', 'I am qiwsir', 'Welcome you', '']

>>> line.split(".",1)  #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']    

>>> name = "Albert Ainstain"  #也有可能用空格来做为分隔符
>>> name.split(" ")
['Albert', 'Ainstain']
"[sep]".join(list)

join可以说是split的逆运算,举例:

>>> name
['Albert', 'Ainstain']
>>> "".join(name)    #将list中的元素连接起来,但是没有连接符,表示一个一个紧邻着
'AlbertAinstain'
>>> ".".join(name)   #以英文的句点做为连接分隔符
'Albert.Ainstain'
>>> " ".join(name)   #以空格做为连接的分隔符
'Albert Ainstain'
Python 相关文章推荐
django模型中的字段和model名显示为中文小技巧分享
Nov 18 Python
Python中字符编码简介、方法及使用建议
Jan 08 Python
使用Py2Exe for Python3创建自己的exe程序示例
Oct 31 Python
Python Pillow.Image 图像保存和参数选择方式
Jan 09 Python
基于Python和PyYAML读取yaml配置文件数据
Jan 13 Python
解决Python命令行下退格,删除,方向键乱码(亲测有效)
Jan 16 Python
python编写俄罗斯方块
Mar 13 Python
基于Python把网站域名解析成ip地址
May 25 Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 Python
Python爬虫之Selenium实现关闭浏览器
Dec 04 Python
python中翻译功能translate模块实现方法
Dec 17 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
Feb 01 Python
Python显示进度条的方法
Sep 20 #Python
python中对list去重的多种方法
Sep 18 #Python
Python中用Descriptor实现类级属性(Property)详解
Sep 18 #Python
Python中的闭包总结
Sep 18 #Python
python的即时标记项目练习笔记
Sep 18 #Python
python脚本实现分析dns日志并对受访域名排行
Sep 18 #Python
python中的字典详细介绍
Sep 18 #Python
You might like
PHP批量删除、清除UTF-8文件BOM头的代码实例
2014/04/14 PHP
php下载文件源代码(强制任意文件格式下载)
2014/05/09 PHP
php实现按天数、星期、月份查询的搜索框
2016/05/02 PHP
一个加密JavaScript的开源工具PACKER2.0.2
2006/11/04 Javascript
js宝典学习笔记(上)
2007/01/10 Javascript
关于javascript document.createDocumentFragment()
2009/04/04 Javascript
jquery DOM操作 基于命令改变页面
2010/05/06 Javascript
javascript利用初始化数据装配模版的实现代码
2010/11/17 Javascript
js静态方法与实例方法分析
2011/07/04 Javascript
js history对象简单实现返回和前进
2013/10/30 Javascript
Bootstrap的Refresh Icon也spin起来
2016/07/13 Javascript
jQuery插件DataTable使用方法详解(.Net平台)
2016/12/22 Javascript
基于jquery二维码生成插件qrcode
2017/01/07 Javascript
JS去掉字符串前后空格、阻止表单提交的实现代码
2017/06/08 Javascript
使用jquery+iframe做一个ajax上传效果(实例)
2017/08/24 jQuery
详解解决Vue相同路由参数不同不会刷新的问题
2018/10/12 Javascript
图文详解vue框架安装步骤
2019/02/12 Javascript
Layui数据表格判断编辑输入的值,是否为我需要的类型详解
2019/10/26 Javascript
微信小程序页面渲染实现方法
2019/11/06 Javascript
Vue 中获取当前时间并实时刷新的实现代码
2020/05/12 Javascript
JS实现拖拽元素时与另一元素碰撞检测
2020/08/27 Javascript
深入讨论Python函数的参数的默认值所引发的问题的原因
2015/03/30 Python
使用python语言,比较两个字符串是否相同的实例
2018/06/29 Python
Python操作多维数组输出和矩阵运算示例
2019/11/28 Python
TensorFlow设置日志级别的几种方式小结
2020/02/04 Python
Python打印不合法的文件名
2020/07/31 Python
python将数据插入数据库的代码分享
2020/08/16 Python
Myprotein亚太地区:欧洲第一在线运动营养品牌
2020/12/20 全球购物
校园报刊亭的创业计划书
2014/01/02 职场文书
探亲邀请信范文
2014/01/30 职场文书
参观接待方案
2014/03/17 职场文书
大学生标准自荐书
2014/06/15 职场文书
另类冲刺标语
2014/06/24 职场文书
2015年世界水日活动总结
2015/02/09 职场文书
2015年新学期寄语
2015/02/26 职场文书
Python中的套接字编程是什么?
2021/06/21 Python