From 4d85ee545c85b3f20d2f73738ec13dd4166e15d3 Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Sun, 31 Mar 2019 10:25:16 +0800 Subject: updated lecture notes --- LectureNotesOnPython.html | 107 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 33 deletions(-) (limited to 'LectureNotesOnPython.html') diff --git a/LectureNotesOnPython.html b/LectureNotesOnPython.html index d8c31eb..3e55a72 100644 --- a/LectureNotesOnPython.html +++ b/LectureNotesOnPython.html @@ -818,12 +818,14 @@ in_both('apples', 'oranges')返回'aes'

+ 操作符用来连接, * 操作符用来重复。

列表的方法

+

append

extend

sort

-

t = ['d', 'c', 'e', 'b', 'a'] -t.sort() # 文t.sort()返回什么值? -t

+

t = ['d', 'c', 'e', 'b', 'a']

+

t.sort() # 问t.sort()返回什么值?

+

t

+

sum - reduce方法,把几个值变成一个值

map方法,把几个值变成另外几个值

@@ -848,27 +850,56 @@ t

pop

-

t = ['a', 'b', 'c'] -x = t.pop(1) # pop可不带参数,不带参数返回哪个值?

-

t = ['a', 'b', 'c'] -del t[1]

-

t = ['a', 'b', 'c', 'd', 'e', 'f'] -del t[1:5]

-

t = ['a', 'b', 'c'] -t.remove('b')

-

list_of_characters = list('spam') -list_of_words = 'spam should be filtered'.split() -list_of_words = 'spam-should-be-filtered'.split('-')

+
+
+
t = ['a', 'b', 'c']
+
x = t.pop(1) # pop可不带参数,不带参数返回哪个值?
+
+
+

del

+
+
+
t = ['a', 'b', 'c']
+
del t[1]
+
+
+
t = ['a', 'b', 'c', 'd', 'e', 'f']
+
del t[1:5]
+
+
+

remove

+
+
+
t = ['a', 'b', 'c']
+
t.remove('b')
+
+
+

split

+
+
+
list_of_characters = list('spam')
+
list_of_words = 'spam should be filtered'.split()
+
list_of_words = 'spam-should-be-filtered'.split('-')
+
+

join方法

-

','.join(['1','2','3'])

-

a = 'banana' -b = 'banana' -a is b # a与b是不是指向同一个值 -a == b

-

a = [1, 2, 3] -b = [1, 2, 3] -a is b # not identical, a and b are not the same object -a == b # equivalent though they have the same values

+
+
+
','.join(['1','2','3'])
+
+
+
a = 'banana'
+
b = 'banana'
+
a is b # a与b是不是指向同一个值
+
a == b
+
+
+
a = [1, 2, 3]
+
b = [1, 2, 3]
+
a is b # not identical, a and b are not the same object
+
a == b # equivalent though they have the same values
+
+

别名(Aliasing)

a = [1, 2, 3] b = a @@ -880,6 +911,7 @@ error-prone(易错)

列表作为参数

+
def delete_head(t):
@@ -891,19 +923,26 @@ error-prone(易错)

delete_head(letters) # letters and t points to the same list object.
letters
+

注意区别 append+ 操作符

-

t1 = [1, 2] -t2 = t1.append(3) -t1 -[1, 2, 3] -t2

-

t3 = t1 + [4] -t3 -[1, 2, 3, 4] -t1 -[1, 2, 3]

+
+
+
t1 = [1, 2]
+
t2 = t1.append(3)
+
t1
+
[1, 2, 3]
+
t2
+
+
+
t3 = t1 + [4]
+
t3
+
[1, 2, 3, 4]
+
t1
+
[1, 2, 3]
+
+

区别如下两个函数:

@@ -918,6 +957,7 @@ t1

字典(Dictionary)

Mutable

超级有用

+
d = {} or d = dict()
@@ -932,6 +972,7 @@ t1
'warm' in d
'温' in d.values()
+

key

value

key-value pair (item)

-- cgit v1.2.1