Skip to content

Commit 6469ac7

Browse files
committed
p3
1 parent e75c420 commit 6469ac7

1 file changed

Lines changed: 60 additions & 29 deletions

File tree

‎114.md‎

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
>Then Peter came to him and said,"Lord, how many times must I forgive my brother who sins against me? As many as seven times?" Jesus said to him,"Not seven times, I tell you, but seventy-seven times?" (MATTHEW 18:21-22)
22
3-
#回顾list和str
3+
#回顾列表和字符串
44

5-
list和str两种类型数据,有不少相似的地方,也有很大的区别。本讲对她们做个简要比较,同时也是对前面有关两者的知识复习一下,所谓“温故而知新”。
5+
列表和字符串两种类型的对象,有不少相似的地方,也有很大的区别。
6+
7+
本讲对她们做个简要比较,同时也是对前面有关两者的知识复习一下,所谓“温故而知新”。
68

79
##相同点
810

9-
###都属于序列类型的数据
11+
###都是序列
12+
13+
不管是组成列表的元素,还是组成字符串的字符,都可以从左向右,依次用`0, 1, 2, ...`这样的方式建立索引。而要得到一个或多个元素,可以使用切片。
1014

11-
所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数减1结束。
15+
关于序列的基本操作,对两者都适用。
1216

1317
例如:
1418

@@ -21,8 +25,9 @@ list和str两种类型数据,有不少相似的地方,也有很大的区别
2125
'u'
2226
>>> welcome_str[:4]
2327
'Welc'
28+
2429
>>> a = "python"
25-
>>> a*3
30+
>>> a * 3
2631
'pythonpythonpython'
2732

2833
>>> git_list = ["qiwsir","github","io"]
@@ -32,8 +37,9 @@ list和str两种类型数据,有不少相似的地方,也有很大的区别
3237
'io'
3338
>>> git_list[0:2]
3439
['qiwsir', 'github']
40+
3541
>>> b = ['qiwsir']
36-
>>> b*7
42+
>>> b * 7
3743
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']
3844

3945
对于此类数据,下面一些操作是类似的:
@@ -63,17 +69,13 @@ list和str两种类型数据,有不少相似的地方,也有很大的区别
6369
>>> len(git_list) #得到元素数
6470
3
6571

66-
另外,前面的讲述中已经说明了关于序列的基本操作,此处不再重复。
67-
6872
##区别
6973

70-
list和str的最大区别是:list是可以改变的,str不可变。这个怎么理解呢?
74+
列表和字符串的最大区别是:列表是可以改变的,字符串是不可变。这个怎么理解呢?
7175

72-
首先看对list的这些操作,其特点是在原处将list进行了修改:
76+
首先看对列表的这些操作,其根源在于列表可以进行修改,即列表是可变的。
7377

74-
>>> git_list
75-
['qiwsir', 'github', 'io']
76-
78+
>>> git_list = ['qiwsir', 'github', 'io']
7779
>>> git_list.append("python")
7880
>>> git_list
7981
['qiwsir', 'github', 'io', 'python']
@@ -84,7 +86,7 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
8486
>>> git_list
8587
['qiwsir', 'github.com', 'io', 'python']
8688

87-
>>> git_list.insert(1,"algorithm")
89+
>>> git_list.insert(1, "algorithm")
8890
>>> git_list
8991
['qiwsir', 'algorithm', 'github.com', 'io', 'python']
9092

@@ -95,7 +97,7 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
9597
>>> git_list
9698
['qiwsir', 'github.com', 'io']
9799

98-
以上这些操作,如果用在str上,都会报错,比如:
100+
以上这些操作,如果用在字符串上,都会报错,比如:
99101

100102
>>> welcome_str
101103
'Welcome you'
@@ -128,12 +130,19 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
128130

129131
##多维list
130132

131-
这个也应该算是两者的区别了,虽然有点牵强。在str中,里面的每个元素只能是字符,在list中,元素可以是任何类型的数据。前面见的多是数字或者字符,其实还可以这样:
133+
这个也应该算是两者的区别了,虽然有点牵强。
134+
135+
在字符串里面的每个元素只能是字符;在列表中,元素可以是任何类型的数据。前面见的多是数字或者字符,其实还可以这样:
136+
137+
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
138+
139+
这个列表的元素,是另外三个列表。这样的列表,称之为多维列表。如果读者学习过行列式,这就比较容易理解了。
132140

133-
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
134-
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
135141
>>> matrix[0][1]
136142
2
143+
144+
当然,列表也可以是这样的:
145+
137146
>>> mult = [[1,2,3],['a','b','c'],'d','e']
138147
>>> mult
139148
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
@@ -142,11 +151,13 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
142151
>>> mult[2]
143152
'd'
144153

145-
以上显示了多维list以及访问方式。在多维的情况下,里面的list被当成一个元素对待。
154+
在多维的情况下,里面的list被当成一个元素对待。
155+
156+
##列表和字符串转化
146157

147-
##list和str转化
158+
符合某些条件的情况下,可以实现列表和字符串之间的转化。会使用到`split()`和`join()`,对这两个函数,已经不陌生了,在前面字符串部分已经见过。
148159

149-
以下涉及到的`split()`和`join()`在前面字符串部分已经见过。一回生,二回熟,这次再见面,特别是在已经学习了列表的基础上,应该有更深刻的理解。
160+
一回生,二回熟,这次再见面,特别是在已经学习了列表的基础上,应该有更深刻的理解。
150161

151162
###str.split()
152163

@@ -156,21 +167,19 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
156167

157168
>>>help(str.split)
158169

159-
得到了对这个内置函数的完整说明。**特别强调:**这是一种非常好的学习方法
160-
161-
>split(...)
162-
>S.split([sep [,maxsplit]]) -> list of strings
170+
split(...)
171+
S.split([sep [,maxsplit]]) -> list of strings
163172
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.
173+
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.
165174

166-
不管是否看懂上面这段话,都可以看例子。还是希望看官能够理解上面的内容。
175+
不管是否看懂上面这段话,都可以看例子。还是希望能够理解上面的内容。
167176

168177
>>> line = "Hello.I am qiwsir.Welcome you."
169178

170179
>>> line.split(".") #以英文的句点为分隔符,得到list
171180
['Hello', 'I am qiwsir', 'Welcome you', '']
172181

173-
>>> line.split(".",1) #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
182+
>>> line.split(".", 1) #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
174183
['Hello', 'I am qiwsir.Welcome you.']
175184

176185
>>> name = "Albert Ainstain" #也有可能用空格来做为分隔符
@@ -190,7 +199,7 @@ list和str的最大区别是:list是可以改变的,str不可变。这个怎
190199

191200
###"[sep]".join(list)
192201

193-
join可以说是split的逆运算,举例:
202+
join可以说是split的逆运算,承接前面的操作:
194203

195204
>>> name
196205
['Albert', 'Ainstain']
@@ -212,6 +221,28 @@ join可以说是split的逆运算,举例:
212221
>>> " ".join(s.split()) #重新连接,不过有一点遗憾,am后面逗号还是有的。怎么去掉?
213222
'I am, writing python book on line'
214223

224+
读者是否感到新奇,对于`join()`函数,其格式是`"sep".join(list)`,不是`list.join(sep)`。其实,`join()`是字符串的方法,不是列表的方法。
225+
226+
>>> help(str.join)
227+
Help on method_descriptor:
228+
229+
join(...)
230+
S.join(iterable) -> str
231+
232+
Return a string which is the concatenation of the strings in the
233+
iterable. The separator between elements is S.
234+
235+
不过,能传入`join()`的对象,或者说参数的值,也是有条件的。下面的就不行。
236+
237+
>>> a = [1,2,3,'a','b','c']
238+
>>> "+".join(a)
239+
Traceback (most recent call last):
240+
File "<pyshell#24>", line 1, in <module>
241+
"+".join(a)
242+
TypeError: sequence item 0: expected str instance, int found
243+
244+
“列表是苦力”,但是暂且让它干这么多。因为更多类型的对象依次登场,先让它到后台。
245+
215246
------
216247

217248
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:列表(3)](./113.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:元组](./115.md)

0 commit comments

Comments
 (0)