跟老齐学Python之list和str比较
作者:hebedich 发布时间:2021-02-26 22:36:36
标签:跟老齐学Python,list,str,比较
相同点
都属于序列类型的数据
所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从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'
0
投稿
猜你喜欢
- 0. 学习目标栈和队列是在程序设计中常见的数据类型,从数据结构的角度来讲,栈和队列也是线性表,是操作受限的线性表,它们的基本操作是线性表操作
- 引子如果遇到了 Must provide secret_key to use csrf错误提醒,原因就是没有设置secret_key ,在代
- 最近,为了能在数据库服务器中运行其他应用程序,在保持数据库操作系统版本不变的前提下对数据库服务器进行了软、硬件上的升级。在软件上,将操作系统
- 一、查询操作可以使用Dataframe的index属性和columns属性获取行、列索引。import pandas as pddata =
- 例子是输出九九乘法表如果按照如下程序写:# 输出九九乘法表 for i in range(10): for j in range
- 导言在前面的教程里我们学习了DataList提供了一些风格样式的属性.而且我们还学习了如何定义HeadStyle, ItemStyle, A
- 本文实例讲述了php解析字符串里所有URL地址的方法。分享给大家供大家参考。具体如下:<?php// $html = the html
- 使用 NetBox 可以方便的将 asp 应用编译成为独立运行的执行程序,完全摆脱 iis 的束缚,在几乎所有的 Windows
- 写在前面的话:此篇还是asp相关的,相信玩ASP的都有这个感觉,当数据有5万多条时-------just like音乐网,要调用最新的10条
- 手头有 109 张头部 CT 的断层扫描图片,我打算用这些图片尝试头部的三维重建。基础工作之一,就是要把这些图片数据读出来,组织成一个三维的
- 近日,有朋友一直打听flash连结服务器相关的知识,搞得我忧心重重,重点是自己也忘记了,大部分Flash的相关开发都是两年前的事,而且fla
- 本文列出了一些asp编程种可能会用到的正则表达式例子,大家可以方便的调用!如果您对asp种的正则表达式不是很了解可以看看这篇文章《ASP中R
- <% String st = ""; for(int i = 1; i <= 9;
- 最近在看python脚本语言,脚本语言是一种解释性的语言,不需要编译,可以直接用,由解释器来负责解释。python语言很强大,而且写起来很简
- 目标:目标文件为一个float32型存储的二进制文件,按列优先方式存储。本文使用Python读取该二进制文件并使用matplotlib.py
- 在一般问题的优化中,最速下降法和共轭梯度法都是非常有用的经典方法,但最速下降法往往以”之”字形下降,速度较慢,不能很快的达到最优值,共轭梯度
- 在已经发表的系列文章中我们已经讨论了两个ASP对象:Application对象和Session对象,因此能够访问Application对象和
- 在html 5增加了新元素header、footer,测试过发现IE不能解析html 5新增的元素。代码如下:<!DOCTYPE&nb
- 网上有很多提供在线按钮制作、文字标题制作、Logo制作服务的网站,它们可以非常方便了让大家轻松的获得效果出色的各类图标型的图片,下面就快来看
- python2中的unicode()函数在python3中会报错:NameError: name 'unicode' is