跟老齐学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 相关文章推荐
Python守护进程(daemon)代码实例
Mar 06 Python
详解字典树Trie结构及其Python代码实现
Jun 03 Python
Python实现压缩与解压gzip大文件的方法
Sep 18 Python
Python 使用requests模块发送GET和POST请求的实现代码
Sep 21 Python
详解Python各大聊天系统的屏蔽脏话功能原理
Dec 01 Python
Django基础之Model操作步骤(介绍)
May 27 Python
通过Python 获取Android设备信息的轻量级框架
Dec 18 Python
Python3 XML 获取雅虎天气的实现方法
Feb 01 Python
django的登录注册系统的示例代码
May 14 Python
Django项目中model的数据处理以及页面交互方法
May 30 Python
详解pyinstaller selenium python3 chrome打包问题
Oct 18 Python
python爬取新闻门户网站的示例
Apr 25 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
Fatal error: Call to undefined function curl_init()解决方法
2010/04/09 PHP
使用PHP计算两个路径的相对路径
2013/06/14 PHP
ThinkPHP 3.2 数据分页代码分享
2014/10/14 PHP
PHP使用glob函数遍历目录或文件夹的方法
2014/12/16 PHP
php使用指定字符列表生成随机字符串的方法
2015/04/18 PHP
JS实现打开本地文件或文件夹
2021/03/09 Javascript
jQuery 加上最后自己的验证
2009/11/04 Javascript
JQuery的一些小应用收集
2010/03/27 Javascript
JavaScript 图像动画的小demo
2012/05/23 Javascript
jQuery实现大转盘抽奖活动仿QQ音乐代码分享
2015/08/21 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
canvas实现图像截取功能
2017/02/06 Javascript
jQuery实现输入框的放大和缩小功能示例
2018/07/21 jQuery
不依任何赖第三方,单纯用vue实现Tree 树形控件的案例
2020/09/21 Javascript
[03:17]DOTA2-DPC中国联赛1月29日Recap集锦
2021/03/11 DOTA
Python进程通信之匿名管道实例讲解
2015/04/11 Python
在Python的setuptools框架下生成egg的教程
2015/04/13 Python
Python Property属性的2种用法
2015/06/21 Python
基于asyncio 异步协程框架实现收集B站直播弹幕
2016/09/11 Python
python3中bytes和string之间的互相转换
2017/02/09 Python
python使用thrift教程的方法示例
2019/03/21 Python
在PYQT5中QscrollArea(滚动条)的使用方法
2019/06/14 Python
django+echart数据动态显示的例子
2019/08/12 Python
PHP统计代码行数的小代码
2019/09/19 Python
Python爬虫之Selenium中frame/iframe表单嵌套页面
2020/12/04 Python
H5离线存储Manifest原理及使用
2020/04/28 HTML / CSS
帕克纽约:PARKER NY
2018/12/09 全球购物
如何在Shell脚本中使用函数
2015/09/06 面试题
绿化先进工作者事迹材料
2014/01/30 职场文书
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
网吧最新创业计划书范文
2014/03/27 职场文书
大学第二课堂活动总结
2014/07/08 职场文书
2014幼儿园教师师德师风演讲稿
2014/09/10 职场文书
拾金不昧表扬稿大全
2015/05/05 职场文书
《三国志》赏析
2019/08/27 职场文书
SQL实现LeetCode(197.上升温度)
2021/08/07 MySQL