跟老齐学Python之list和str比较
2023-06-13 09:15:46 时间
相同点
都属于序列类型的数据
所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数减1结束。
例如:
>>>welcome_str="Welcomeyou" >>>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 "Welcomeyou" >>>first+","+welcome_str#用+号连接str "hello,world,Welcomeyou" >>>welcome_str#原来的str没有受到影响,即上面的+号连接后从新生成了一个字符串 "Welcomeyou" >>>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" >>>delgit_list[1] >>>git_list ["qiwsir","github.com","io"]
以上这些操作,如果用在str上,都会报错,比如:
>>>welcome_str "Welcomeyou" >>>welcome_str[1]="E" Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> TypeError:"str"objectdoesnotsupportitemassignment >>>delwelcome_str[1] Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> TypeError:"str"objectdoesn"tsupportitemdeletion >>>welcome_str.append("E") Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> AttributeError:"str"objecthasnoattribute"append"
如果要修改一个str,不得不这样。
>>>welcome_str "Welcomeyou" >>>welcome_str[0]+"E"+welcome_str[2:]#从新生成一个str "WElcomeyou" >>>welcome_str#对原来的没有任何影响 "Welcomeyou"
其实,在这种做法中,相当于从新生成了一个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]])->listofstrings ReturnalistofthewordsinthestringS,usingsepasthedelimiterstring.Ifmaxsplitisgiven,atmostmaxsplitsplitsaredone.IfsepisnotspecifiedorisNone,anywhitespacestringisaseparatorandemptystringsareremovedfromtheresult.
不管是否看懂上面这段话,都可以看例子。还是希望看官能够理解上面的内容。
>>>line="Hello.Iamqiwsir.Welcomeyou." >>>line.split(".")#以英文的句点为分隔符,得到list ["Hello","Iamqiwsir","Welcomeyou",""] >>>line.split(".",1)#这个1,就是表达了上文中的:Ifmaxsplitisgiven,atmostmaxsplitsplitsaredone. ["Hello","Iamqiwsir.Welcomeyou."] >>>name="AlbertAinstain"#也有可能用空格来做为分隔符 >>>name.split("") ["Albert","Ainstain"] "[sep]".join(list)
join可以说是split的逆运算,举例:
>>>name ["Albert","Ainstain"] >>>"".join(name)#将list中的元素连接起来,但是没有连接符,表示一个一个紧邻着 "AlbertAinstain" >>>".".join(name)#以英文的句点做为连接分隔符 "Albert.Ainstain" >>>"".join(name)#以空格做为连接的分隔符 "AlbertAinstain"
相关文章
- Python多线程通信_python socket多线程
- Python – 实现矩阵转置
- 浙江新增python编程_9月起,浙江省八年级新增Python编程课,未来编程是处理大数据的手段…「建议收藏」
- Python删除文件中重复的内容「建议收藏」
- python整除和取余写法_Python的整除和取余[通俗易懂]
- python安装不了whl文件_Python安装whl文件过程图解
- python删除首行_Python删除文件第一行
- Python设置环境变量
- 【说站】python序列操作的整理
- 【说站】Python yield关键字的应用限制
- 【说站】Python requests如何发送请求
- 【说站】python中进程池Pool的初始化
- 【说站】python异常时的语句处理
- 【说站】Python中cProfile分析工具的使用
- lambda表达式python_Python中的Lambda表达式「建议收藏」
- 在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包
- python type error是什么意思_Python 报错 TypeError:’DoesNotExist’对象不可调用
- 力扣刷题笔记--14. 最长公共前缀 python解法
- Python爬取全市场基金持仓,扒一扒基金经理们的调仓选股思路
- Python最全最常用的日期工具类(随时更新)
- python-Python与MongoDB数据库-处理MongoDB查询结果
- Python学习:6.python内置函数详解编程语言
- python_day01-和python有关的唠嗑详解编程语言
- LIST-PROCESSING用法 ABAP任意时刻进行List输出_SAP详解编程语言
- python截图方法详解编程语言
- Hibernate Criteria接口 list方法:返回一个List集合
- 一步步学习:利用Python连接MySQL数据库(python连接mysql数据库)
- python中list循环语句用法实例
- python中的五种异常处理机制介绍