Skip to content

Commit 54fd982

Browse files
committed
list and str
1 parent 70390d4 commit 54fd982

2 files changed

Lines changed: 220 additions & 0 deletions

File tree

‎114.md‎

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
>“若有人要跟从我,就当舍己,背起他的十字架,来跟从我。因为凡要救自己生命的,必丧掉生命;凡为我丧掉生命的,必得着生命。人若赚得全世界,赔上自己的生命,有什么益处呢?人还能拿什么换生命呢?”(MATTHEW 32:24-26)
2+
3+
#回顾list和str
4+
5+
list和str两种类型数据,有不少相似的地方,也有很大的区别。本讲对她们做个简要比较,同时也是对前面有关两者的知识复习一下,所谓“温故而知新”。
6+
7+
##相同点
8+
9+
###都属于序列类型的数据
10+
11+
所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数减1结束。
12+
13+
例如:
14+
15+
>>> welcome_str = "Welcome you"
16+
>>> welcome_str[0]
17+
'W'
18+
>>> welcome_str[1]
19+
'e'
20+
>>> welcome_str[len(welcome_str)-1]
21+
'u'
22+
>>> welcome_str[:4]
23+
'Welc'
24+
>>> a = "python"
25+
>>> a*3
26+
'pythonpythonpython'
27+
28+
>>> git_list = ["qiwsir","github","io"]
29+
>>> git_list[0]
30+
'qiwsir'
31+
>>> git_list[len(git_list)-1]
32+
'io'
33+
>>> git_list[0:2]
34+
['qiwsir', 'github']
35+
>>> b = ['qiwsir']
36+
>>> b*7
37+
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']
38+
39+
对于此类数据,下面一些操作是类似的:
40+
41+
>>> first = "hello,world"
42+
>>> welcome_str
43+
'Welcome you'
44+
>>> first+","+welcome_str #用+号连接str
45+
'hello,world,Welcome you'
46+
>>> welcome_str #原来的str没有受到影响,即上面的+号连接后从新生成了一个字符串
47+
'Welcome you'
48+
>>> first
49+
'hello,world'
50+
51+
>>> language = ['python']
52+
>>> git_list
53+
['qiwsir', 'github', 'io']
54+
>>> language + git_list #用+号连接list,得到一个新的list
55+
['python', 'qiwsir', 'github', 'io']
56+
>>> git_list
57+
['qiwsir', 'github', 'io']
58+
>>> language
59+
['python']
60+
61+
>>> len(welcome_str) #得到字符数
62+
11
63+
>>> len(git_list) #得到元素数
64+
3
65+
66+
另外,前面的讲述中已经说明了关于序列的基本操作,此处不再重复。
67+
68+
##区别
69+
70+
list和str的最大区别是:list是可以改变的,str不可变。这个怎么理解呢?
71+
72+
首先看对list的这些操作,其特点是在原处将list进行了修改:
73+
74+
>>> git_list
75+
['qiwsir', 'github', 'io']
76+
77+
>>> git_list.append("python")
78+
>>> git_list
79+
['qiwsir', 'github', 'io', 'python']
80+
81+
>>> git_list[1]
82+
'github'
83+
>>> git_list[1] = 'github.com'
84+
>>> git_list
85+
['qiwsir', 'github.com', 'io', 'python']
86+
87+
>>> git_list.insert(1,"algorithm")
88+
>>> git_list
89+
['qiwsir', 'algorithm', 'github.com', 'io', 'python']
90+
91+
>>> git_list.pop()
92+
'python'
93+
94+
>>> del git_list[1]
95+
>>> git_list
96+
['qiwsir', 'github.com', 'io']
97+
98+
以上这些操作,如果用在str上,都会报错,比如:
99+
100+
>>> welcome_str
101+
'Welcome you'
102+
103+
>>> welcome_str[1]='E'
104+
Traceback (most recent call last):
105+
File "<stdin>", line 1, in <module>
106+
TypeError: 'str' object does not support item assignment
107+
108+
>>> del welcome_str[1]
109+
Traceback (most recent call last):
110+
File "<stdin>", line 1, in <module>
111+
TypeError: 'str' object doesn't support item deletion
112+
113+
>>> welcome_str.append("E")
114+
Traceback (most recent call last):
115+
File "<stdin>", line 1, in <module>
116+
AttributeError: 'str' object has no attribute 'append'
117+
118+
如果要修改一个str,不得不这样。
119+
120+
>>> welcome_str
121+
'Welcome you'
122+
>>> welcome_str[0]+"E"+welcome_str[2:] #从新生成一个str
123+
'WElcome you'
124+
>>> welcome_str #对原来的没有任何影响
125+
'Welcome you'
126+
127+
其实,在这种做法中,相当于从新生成了一个str。
128+
129+
##多维list
130+
131+
这个也应该算是两者的区别了,虽然有点牵强。在str中,里面的每个元素只能是字符,在list中,元素可以是任何类型的数据。前面见的多是数字或者字符,其实还可以这样:
132+
133+
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
134+
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
135+
>>> matrix[0][1]
136+
2
137+
>>> mult = [[1,2,3],['a','b','c'],'d','e']
138+
>>> mult
139+
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
140+
>>> mult[1][1]
141+
'b'
142+
>>> mult[2]
143+
'd'
144+
145+
以上显示了多维list以及访问方式。在多维的情况下,里面的list也跟一个前面元素一样对待。
146+
147+
##list和str转化
148+
149+
以下涉及到的`split()`和`join()`在前面字符串部分已经见过。一回生,二回熟,这次再见面,特别是在已经学习了列表的基础上,应该有更深刻的理解。
150+
151+
###str.split()
152+
153+
这个内置函数实现的是将str转化为list。其中str=""是分隔符。
154+
155+
在看例子之前,请看官在交互模式下做如下操作:
156+
157+
>>>help(str.split)
158+
159+
得到了对这个内置函数的完整说明。**特别强调:**这是一种非常好的学习方法
160+
161+
>split(...)
162+
>S.split([sep [,maxsplit]]) -> list of strings
163+
164+
>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.
165+
166+
不管是否看懂上面这段话,都可以看例子。还是希望看官能够理解上面的内容。
167+
168+
>>> line = "Hello.I am qiwsir.Welcome you."
169+
170+
>>> line.split(".") #以英文的句点为分隔符,得到list
171+
['Hello', 'I am qiwsir', 'Welcome you', '']
172+
173+
>>> line.split(".",1) #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
174+
['Hello', 'I am qiwsir.Welcome you.']
175+
176+
>>> name = "Albert Ainstain" #也有可能用空格来做为分隔符
177+
>>> name.split(" ")
178+
['Albert', 'Ainstain']
179+
180+
下面的例子,让你更有点惊奇了。
181+
182+
>>> s = "I am, writing\npython\tbook on line" #这个字符串中有空格,逗号,换行\n,tab缩进\t 符号
183+
>>> print s #输出之后的样式
184+
I am, writing
185+
python book on line
186+
>>> s.split() #用split(),但是括号中不输入任何参数
187+
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
188+
189+
如果split()不输入任何参数,显示就是见到任何分割符号,就用其分割了。
190+
191+
###"[sep]".join(list)
192+
193+
join可以说是split的逆运算,举例:
194+
195+
>>> name
196+
['Albert', 'Ainstain']
197+
>>> "".join(name) #将list中的元素连接起来,但是没有连接符,表示一个一个紧邻着
198+
'AlbertAinstain'
199+
>>> ".".join(name) #以英文的句点做为连接分隔符
200+
'Albert.Ainstain'
201+
>>> " ".join(name) #以空格做为连接的分隔符
202+
'Albert Ainstain'
203+
204+
回到上面那个神奇的例子中,可以这么使用join.
205+
206+
>>> s = "I am, writing\npython\tbook on line"
207+
>>> print s
208+
I am, writing
209+
python book on line
210+
>>> s.split()
211+
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
212+
>>> " ".join(s.split()) #重新连接,不过有一点遗憾,am后面逗号还是有的。怎么去掉?
213+
'I am, writing python book on line'
214+
215+
------
216+
217+
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:列表(3)](./113.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:字典](./115.md)
218+
219+
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。

‎index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
11. [列表(1)](./111.md)==>列表定义,索引和切片,列表反转,元素追加,基本操作
2626
12. [列表(2)](./112.md)==>列表append/extend/index/count方法,可迭代的和判断方法,列表原地修改
2727
13. [列表(3)](./113.md)==>列表pop/remove/reverse/sort方法
28+
14. [回顾列表和字符串](./114.md)==>比较列表和字符串的相同点和不同点
2829

2930
##第二部分:函数和类
3031

0 commit comments

Comments
 (0)