跟老齐学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 相关文章推荐
基于scrapy实现的简单蜘蛛采集程序
Apr 17 Python
python操作 hbase 数据的方法
Dec 18 Python
Python线性回归实战分析
Feb 01 Python
Python查找最长不包含重复字符的子字符串算法示例
Feb 13 Python
Python3使用Matplotlib 绘制精美的数学函数图形
Apr 11 Python
django写用户登录判定并跳转制定页面的实例
Aug 21 Python
Python进程间通信 multiProcessing Queue队列实现详解
Sep 23 Python
python3中关于excel追加写入格式被覆盖问题(实例代码)
Jan 10 Python
python实现扫雷小游戏
Apr 24 Python
Python中socket网络通信是干嘛的
May 27 Python
Python连接mysql方法及常用参数
Sep 01 Python
python多线程爬取西刺代理的示例代码
Jan 30 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
非常不错的MySQL优化的8条经验
2008/03/24 PHP
通用PHP动态生成静态HTML网页的代码
2010/03/04 PHP
QQ登录 PHP OAuth示例代码
2011/07/20 PHP
Codeigniter生成Excel文档的简单方法
2014/06/12 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
PHP自定义错误处理的方法分析
2018/12/19 PHP
jQuery Ajax 全解析
2009/02/08 Javascript
jQuery 1.2.x 升? 1.3.x 注意事项
2009/05/06 Javascript
jquery下div 的resize事件示例代码
2014/03/09 Javascript
Node.js中require的工作原理浅析
2014/06/24 Javascript
node.js [superAgent] 请求使用示例
2015/03/13 Javascript
JS实现控制表格内指定单元格内容对齐的方法
2015/03/30 Javascript
理解javascript中的with关键字
2016/02/15 Javascript
在JavaScript中调用Java类和接口的方法
2016/09/07 Javascript
jQuery点击弹出层弹出模态框点击模态框消失代码分享
2017/01/21 Javascript
JS实现简单表格排序操作示例
2017/10/07 Javascript
Vue面试题及Vue知识点整理
2018/10/07 Javascript
js中script的上下放置区别,Dom的增删改创建操作实例分析
2019/12/16 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
[10:04]国际邀请赛采访专栏:DK.Farseer,mouz.Black^,采访员Josh专访
2013/08/05 DOTA
[04:27]2014DOTA2国际邀请赛 NAVI战队官方纪录片
2014/07/21 DOTA
[48:32]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 LGD vs VG
2018/04/01 DOTA
python 提取文件的小程序
2009/07/29 Python
Python的网络编程库Gevent的安装及使用技巧
2016/06/24 Python
python 通过xml获取测试节点和属性的实例
2018/03/31 Python
python实现键盘控制鼠标移动
2020/11/27 Python
python 检查是否为中文字符串的方法
2018/12/28 Python
django自定义模板标签过程解析
2019/12/14 Python
win10从零安装配置pytorch全过程图文详解
2020/05/08 Python
详解Django自定义图片和文件上传路径(upload_to)的2种方式
2020/12/01 Python
英国森林假期:Forest Holidays
2021/01/01 全球购物
小学少先队活动方案
2014/02/18 职场文书
开学典礼策划方案
2014/05/28 职场文书
励志演讲稿300字
2014/08/21 职场文书
儿子满月酒致辞
2015/07/29 职场文书
MySQL中int (10) 和 int (11) 的区别
2022/01/22 MySQL