列表
简单数据类型
- 整型
<class 'int'>
- 浮点型
<class 'float'>
- 布尔型
<class 'bool'>
容器数据类型
- 列表
<class 'list'>
- 元组
<class 'tuple'>
- 字典
<class 'dict'>
- 集合
<class 'set'>
- 字符串
<class 'str'>
1. 列表的定义
列表是有序集合,没有固定大小,能够保存任意数量任意类型的 Python 对象,语法为 [元素1, 元素2, ..., 元素n]
。
- 关键点是「中括号 []」和「逗号 ,」
- 中括号 把所有元素绑在一起
- 逗号 将每个元素一一分开
2. 列表的创建
- 创建一个普通列表
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(x, type(x))
3
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] <class 'list'>
4
5
x = [2, 3, 4, 5, 6, 7]
6
print(x, type(x))
7
# [2, 3, 4, 5, 6, 7] <class 'list'>
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] <class 'list'> [2, 3, 4, 5, 6, 7] <class 'list'>
- 利用
range()
创建列表
【例子】
1
x = list(range(10))
2
print(x, type(x))
3
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
4
5
x = list(range(1, 11, 2))
6
print(x, type(x))
7
# [1, 3, 5, 7, 9] <class 'list'>
8
9
x = list(range(10, 1, -2))
10
print(x, type(x))
11
# [10, 8, 6, 4, 2] <class 'list'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'> [1, 3, 5, 7, 9] <class 'list'> [10, 8, 6, 4, 2] <class 'list'>
- 利用推导式创建列表
【例子】
1
x = [0] * 5
2
print(x, type(x))
3
# [0, 0, 0, 0, 0] <class 'list'>
4
5
x = [0 for i in range(5)]
6
print(x, type(x))
7
# [0, 0, 0, 0, 0] <class 'list'>
8
9
x = [i for i in range(10)]
10
print(x, type(x))
11
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
12
13
x = [i for i in range(1, 10, 2)]
14
print(x, type(x))
15
# [1, 3, 5, 7, 9] <class 'list'>
16
17
x = [i for i in range(10, 1, -2)]
18
print(x, type(x))
19
# [10, 8, 6, 4, 2] <class 'list'>
20
21
x = [i ** 2 for i in range(1, 10)]
22
print(x, type(x))
23
# [1, 4, 9, 16, 25, 36, 49, 64, 81] <class 'list'>
24
25
x = [i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]
26
print(x, type(x))
27
28
# [3, 9, 15, 21, 27, 33, 39,
[0, 0, 0, 0, 0] <class 'list'> [0, 0, 0, 0, 0] <class 'list'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'> [1, 3, 5, 7, 9] <class 'list'> [10, 8, 6, 4, 2] <class 'list'> [1, 4, 9, 16, 25, 36, 49, 64, 81] <class 'list'> [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99] <class 'list'>
注意:
由于list的元素可以是任何对象,因此列表中所保存的是对象的指针。即使保存一个简单的[1,2,3]
,也有3个指针和3个整数对象。
x = [a] * 4
操作中,只是创建4个指向list的引用,所以一旦a
改变,x
中4个a
也会随之改变。
【例子】
1
x = [[0] * 3] * 4
2
print(x, type(x))
3
# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>
4
5
x[0][0] = 1
6
print(x, type(x))
7
# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>
8
9
a = [0] * 3
10
x = [a] * 4
11
print(x, type(x))
12
# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>
13
14
x[0][0] = 1
15
print(x, type(x))
16
# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'> [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'> [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'> [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>
- 创建一个混合列表
【例子】
1
mix = [1, 'lsgo', 3.14, [1, 2, 3]]
2
print(mix, type(mix))
3
# [1, 'lsgo', 3.14, [1, 2, 3]] <class 'list'>
[1, 'lsgo', 3.14, [1, 2, 3]] <class 'list'>
- 创建一个空列表
【例子】
1
empty = []
2
print(empty, type(empty)) # [] <class 'list'>
[] <class 'list'>
列表不像元组,列表内容可更改 (mutable),因此附加 (append
, extend
)、插入 (insert
)、删除 (remove
, pop
) 这些操作都可以用在它身上。
3. 向列表中添加元素
list.append(obj)
在列表末尾添加新的对象,只接受一个参数,参数可以是任何数据类型,被追加的元素在 list 中保持着原结构类型。
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
x.append('Thursday')
3
print(x)
4
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday']
5
6
print(len(x)) # 6
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday'] 6
此元素如果是一个 list,那么这个 list 将作为一个整体进行追加,注意append()
和extend()
的区别。
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
x.append(['Thursday', 'Sunday'])
3
print(x)
4
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', ['Thursday', 'Sunday']]
5
6
print(len(x)) # 6
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', ['Thursday', 'Sunday']] 6
list.extend(seq)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
x.extend(['Thursday', 'Sunday'])
3
print(x)
4
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']
5
6
print(len(x)) # 7
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday'] 7
严格来说 append
是追加,把一个东西整体添加在列表后,而 extend
是扩展,把一个东西里的所有元素添加在列表后。
list.insert(index, obj)
在编号index
位置插入obj
。
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
x.insert(2, 'Sunday')
3
print(x)
4
# ['Monday', 'Tuesday', 'Sunday', 'Wednesday', 'Thursday', 'Friday']
5
6
print(len(x)) # 6
['Monday', 'Tuesday', 'Sunday', 'Wednesday', 'Thursday', 'Friday'] 6
4. 删除列表中的元素
list.remove(obj)
移除列表中某个值的第一个匹配项
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
x.remove('Monday')
3
print(x) # ['Tuesday', 'Wednesday', 'Thursday', 'Friday']
['Tuesday', 'Wednesday', 'Thursday', 'Friday']
list.pop([index=-1])
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
y = x.pop()
3
print(y) # Friday
4
5
y = x.pop(0)
6
print(y) # Monday
7
8
y = x.pop(-2)
9
print(y) # Wednesday
10
print(x) # ['Tuesday', 'Thursday']
Friday Monday Wednesday ['Tuesday', 'Thursday']
remove
和 pop
都可以删除元素,前者是指定具体要删除的元素,后者是指定一个索引。
del var1[, var2 ……]
删除单个或多个对象。
【例子】
如果知道要删除的元素在列表中的位置,可使用del
语句。
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
del x[0:2]
3
print(x) # ['Wednesday', 'Thursday', 'Friday']
['Wednesday', 'Thursday', 'Friday']
如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del
语句;如果你要在删除元素后还能继续使用它,就使用方法pop()
。
5. 获取列表中的元素
- 通过元素的索引值,从列表获取单个元素,注意,列表索引值是从0开始的。
- 通过将索引指定为-1,可让Python返回最后一个列表元素,索引 -2 返回倒数第二个列表元素,以此类推。
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', ['Thursday', 'Friday']]
2
print(x[0], type(x[0])) # Monday <class 'str'>
3
print(x[-1], type(x[-1])) # ['Thursday', 'Friday'] <class 'list'>
4
print(x[-2], type(x[-2])) # Wednesday <class 'str'>
Monday <class 'str'> ['Thursday', 'Friday'] <class 'list'> Wednesday <class 'str'>
切片的通用写法是 start : stop : step
- 情况 1 - "start :"
- 以
step
为 1 (默认) 从编号start
往列表尾部切片。
【例子】
1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(x[3:]) # ['Thursday', 'Friday']
3
print(x[-3:]) # ['Wednesday', 'Thursday', 'Friday']
['Thursday', 'Friday'] ['Wednesday', 'Thursday', 'Friday']
- 情况 2 - ": stop"
- 以
step
为 1 (默认) 从列表头部往编号stop
切片。
【例子】
1
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(week[:3]) # ['Monday', 'Tuesday', 'Wednesday']
3
print(week[:-3]) # ['Monday', 'Tuesday']
['Monday', 'Tuesday', 'Wednesday'] ['Monday', 'Tuesday']
- 情况 3 - "start : stop"
- 以
step
为 1 (默认) 从编号start
往编号stop
切片。
【例子】
1
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(week[1:3]) # ['Tuesday', 'Wednesday']
3
print(week[-3:-1]) # ['Wednesday', 'Thursday']
['Tuesday', 'Wednesday'] ['Wednesday', 'Thursday']
- 情况 4 - "start : stop : step"
- 以具体的
step
从编号start
往编号stop
切片。注意最后把step
设为 -1,相当于将列表反向排列。
【例子】
1
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(week[1:4:2]) # ['Tuesday', 'Thursday']
3
print(week[:4:2]) # ['Monday', 'Wednesday']
4
print(week[1::2]) # ['Tuesday', 'Thursday']
5
print(week[::-1])
6
# ['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']
['Tuesday', 'Thursday'] ['Monday', 'Wednesday'] ['Tuesday', 'Thursday'] ['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']
- 情况 5 - " : "
- 复制列表中的所有元素(浅拷贝)。
【例子】
1
eek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
2
print(week[:])
3
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
【例子】浅拷贝与深拷贝
1
list1 = [123, 456, 789, 213]
2
list2 = list1
3
list3 = list1[:]
4
5
print(list2) # [123, 456, 789, 213]
6
print(list3) # [123, 456, 789, 213]
7
list1.sort()
8
print(list2) # [123, 213, 456, 789]
9
print(list3) # [123, 456, 789, 213]
10
11
list1 = [[123, 456], [789, 213]]
12
list2 = list1
13
list3 = list1[:]
14
print(list2) # [[123, 456], [789, 213]]
15
print(list3) # [[123, 456], [789, 213]]
16
list1[0][0] = 111
17
print(list2) # [[111, 456], [789, 213]]
18
print(list3) # [[111, 456], [789, 213]]
[123, 456, 789, 213] [123, 456, 789, 213] [123, 213, 456, 789] [123, 456, 789, 213] [[123, 456], [789, 213]] [[123, 456], [789, 213]] [[111, 456], [789, 213]] [[111, 456], [789, 213]]
6. 列表的常用操作符
- 等号操作符:
==
- 连接操作符
+
- 重复操作符
*
- 成员关系操作符
in
、not in
「等号 ==」,只有成员、成员位置都相同时才返回True。
列表拼接有两种方式,用「加号 +」和「乘号 *」,前者首尾拼接,后者复制拼接。
【例子】
1
list1 = [123, 456]
2
list2 = [456, 123]
3
list3 = [123, 456]
4
5
print(list1 == list2) # False
6
print(list1 == list3) # True
7
8
list4 = list1 + list2 # extend()
9
print(list4) # [123, 456, 456, 123]
10
11
list5 = list3 * 3
12
print(list5) # [123, 456, 123, 456, 123, 456]
13
14
list3 *= 3
15
print(list3) # [123, 456, 123, 456, 123, 456]
16
17
print(123 in list3) # True
18
print(456 not in list3) # False
False True [123, 456, 456, 123] [123, 456, 123, 456, 123, 456] [123, 456, 123, 456, 123, 456] True False
前面三种方法(append
, extend
, insert
)可对列表增加元素,它们没有返回值,是直接修改了原数据对象。 而将两个list相加,需要创建新的 list 对象,从而需要消耗额外的内存,特别是当 list 较大时,尽量不要使用 “+” 来添加list。
7. 列表的其它方法
list.count(obj)
统计某个元素在列表中出现的次数
【例子】
1
list1 = [123, 456] * 3
2
print(list1) # [123, 456, 123, 456, 123, 456]
3
num = list1.count(123)
4
print(num) # 3
[123, 456, 123, 456, 123, 456] 3
list.index(x[, start[, end]])
从列表中找出某个值第一个匹配项的索引位置
【例子】
1
list1 = [123, 456] * 5
2
print(list1.index(123)) # 0
3
print(list1.index(123, 1)) # 2
4
print(list1.index(123, 3, 7)) # 4
0 2 4
list.reverse()
反向列表中元素
【例子】
1
x = [123, 456, 789]
2
x.reverse()
3
print(x) # [789, 456, 123]
[789, 456, 123]
list.sort(key=None, reverse=False)
对原列表进行排序。
key
-- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。reverse
-- 排序规则,reverse = True
降序,reverse = False
升序(默认)。- 该方法没有返回值,但是会对列表的对象进行排序。
【例子】
1
x = [123, 456, 789, 213]
2
x.sort()
3
print(x)
4
# [123, 213, 456, 789]
5
6
x.sort(reverse=True)
7
print(x)
8
# [789, 456, 213, 123]
9
10
11
# 获取列表的第二个元素
12
def takeSecond(elem):
13
return elem[1]
14
15
16
x = [(2, 2), (3, 4), (4, 1), (1, 3)]
17
x.sort(key=takeSecond)
18
print(x)
19
# [(4, 1), (2, 2), (1, 3), (3, 4)]
20
21
x.sort(key=lambda a: a[0])
22
print(x)
23
# [(1, 3), (2, 2), (3, 4), (4, 1)]
[123, 213, 456, 789] [789, 456, 213, 123] [(4, 1), (2, 2), (1, 3), (3, 4)] [(1, 3), (2, 2), (3, 4), (4, 1)]
元组
「元组」定义语法为:(元素1, 元素2, ..., 元素n)
- 小括号把所有元素绑在一起
- 逗号将每个元素一一分开
1. 创建和访问一个元组
- Python 的元组与列表类似,不同之处在于tuple被创建后就不能对其进行修改,类似字符串。
- 元组使用小括号,列表使用方括号。
- 元组与列表类似,也用整数来对它进行索引 (indexing) 和切片 (slicing)。
【例子】
1
t1 = (1, 10.31, 'python')
2
t2 = 1, 10.31, 'python'
3
print(t1, type(t1))
4
# (1, 10.31, 'python') <class 'tuple'>
5
6
print(t2, type(t2))
7
# (1, 10.31, 'python') <class 'tuple'>
8
9
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)
10
print(tuple1[1]) # 2
11
print(tuple1[5:]) # (6, 7, 8)
12
print(tuple1[:5]) # (1, 2, 3, 4, 5)
13
tuple2 = tuple1[:]
14
print(tuple2) # (1, 2, 3, 4, 5, 6, 7, 8)
(1, 10.31, 'python') <class 'tuple'> (1, 10.31, 'python') <class 'tuple'> 2 (6, 7, 8) (1, 2, 3, 4, 5) (1, 2, 3, 4, 5, 6, 7, 8)
- 创建元组可以用小括号 (),也可以什么都不用,为了可读性,建议还是用 ()。
- 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用。
【例子】
1
x = (1)
2
print(type(x)) # <class 'int'>
3
x = 2, 3, 4, 5
4
print(type(x)) # <class 'tuple'>
5
x = []
6
print(type(x)) # <class 'list'>
7
x = ()
8
print(type(x)) # <class 'tuple'>
9
x = (1,)
10
print(type(x)) # <class 'tuple'>
<class 'int'> <class 'tuple'> <class 'list'> <class 'tuple'> <class 'tuple'>
【例子】
1
print(8 * (8)) # 64
2
print(8 * (8,)) # (8, 8, 8, 8, 8, 8, 8, 8)
64 (8, 8, 8, 8, 8, 8, 8, 8)
【例子】创建二维元组。
1
x = (1, 10.31, 'python'), ('data', 11)
2
print(x)
3
# ((1, 10.31, 'python'), ('data', 11))
4
5
print(x[0])
6
# (1, 10.31, 'python')
7
print(x[0][0], x[0][1], x[0][2])
8
# 1 10.31 python
9
10
print(x[0][0:2])
11
# (1, 10.31)
((1, 10.31, 'python'), ('data', 11)) (1, 10.31, 'python') 1 10.31 python (1, 10.31)
2. 更新和删除一个元组
【例子】
1
week = ('Monday', 'Tuesday', 'Thursday', 'Friday')
2
week = week[:2] + ('Wednesday',) + week[2:]
3
print(week) # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
【例子】元组有不可更改 (immutable) 的性质,因此不能直接给元组的元素赋值,但是只要元组中的元素可更改 (mutable),那么我们可以直接更改其元素,注意这跟赋值其元素不同。
1
t1 = (1, 2, 3, [4, 5, 6])
2
print(t1) # (1, 2, 3, [4, 5, 6])
3
4
t1[3][0] = 9
5
print(t1) # (1, 2, 3, [9, 5, 6])
(1, 2, 3, [4, 5, 6]) (1, 2, 3, [9, 5, 6])
3. 元组相关的操作符
- 等号操作符:
==
- 连接操作符
+
- 重复操作符
*
- 成员关系操作符
in
、not in
「等号 ==」,只有成员、成员位置都相同时才返回True。
元组拼接有两种方式,用「加号 +」和「乘号 *」,前者首尾拼接,后者复制拼接。
【例子】
1
t1 = (123, 456)
2
t2 = (456, 123)
3
t3 = (123, 456)
4
5
print(t1 == t2) # False
6
print(t1 == t3) # True
7
8
t4 = t1 + t2
9
print(t4) # (123, 456, 456, 123)
10
11
t5 = t3 * 3
12
print(t5) # (123, 456, 123, 456, 123, 456)
13
14
t3 *= 3
15
print(t3) # (123, 456, 123, 456, 123, 456)
16
17
print(123 in t3) # True
18
print(456 not in t3) # False
False True (123, 456, 456, 123) (123, 456, 123, 456, 123, 456) (123, 456, 123, 456, 123, 456) True False
4. 内置方法
元组大小和内容都不可更改,因此只有 count
和 index
两种方法。
【例子】
1
t = (1, 10.31, 'python')
2
print(t.count('python')) # 1
3
print(t.index(10.31)) # 1
1 1
count('python')
是记录在元组t
中该元素出现几次,显然是 1 次index(10.31)
是找到该元素在元组t
的索引,显然是 1
5. 解压元组
【例子】解压(unpack)一维元组(有几个元素左边括号定义几个变量)
1
t = (1, 10.31, 'python')
2
(a, b, c) = t
3
print(a, b, c)
4
# 1 10.31 python
1 10.31 python
【例子】解压二维元组(按照元组里的元组结构来定义变量)
1
t = (1, 10.31, ('OK', 'python'))
2
(a, b, (c, d)) = t
3
print(a, b, c, d)
4
# 1 10.31 OK python
1 10.31 OK python
【例子】如果你只想要元组其中几个元素,用通配符「*」,英文叫 wildcard,在计算机语言中代表一个或多个元素。下例就是把多个元素丢给了 rest
变量。
1
t = 1, 2, 3, 4, 5
2
a, b, *rest, c = t
3
print(a, b, c) # 1 2 5
4
print(rest) # [3, 4]
1 2 5 [3, 4]
【例子】如果你根本不在乎 rest 变量,那么就用通配符「*」加上下划线「_」。
1
t = 1, 2, 3, 4, 5
2
a, b, *_ = t
3
print(a, b) # 1 2
1 2
字符串
1. 字符串的定义
- Python 中字符串被定义为引号之间的字符集合。
- Python 支持使用成对的 单引号 或 双引号。
【例子】
1
t1 = 'i love Python!'
2
print(t1, type(t1))
3
# i love Python! <class 'str'>
4
5
t2 = "I love Python!"
6
print(t2, type(t2))
7
# I love Python! <class 'str'>
8
9
print(5 + 8) # 13
10
print('5' + '8') # 58
i love Python! <class 'str'> I love Python! <class 'str'> 13 58
- Python 的常用转义字符
转义字符 | 描述 |
---|---|
\\ | 反斜杠符号 |
\' | 单引号 |
\" | 双引号 |
\n | 换行 |
\t | 横向制表符(TAB) |
\r | 回车 |
【例子】如果字符串中需要出现单引号或双引号,可以使用转义符号\
对字符串中的符号进行转义。
1
print('let\'s go') # let's go
2
print("let's go") # let's go
3
print('C:\\now') # C:\now
4
print("C:\\Program Files\\Intel\\Wifi\\Help")
5
# C:\Program Files\Intel\Wifi\Help
let's go let's go C:\now C:\Program Files\Intel\Wifi\Help
【例子】原始字符串只需要在字符串前边加一个英文字母 r 即可。
1
print(r'C:\Program Files\Intel\Wifi\Help')
2
# C:\Program Files\Intel\Wifi\Help
C:\Program Files\Intel\Wifi\Help
【例子】三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
1
para_str = """这是一个多行字符串的实例
2
多行字符串可以使用制表符
3
TAB ( \t )。
4
也可以使用换行符 [ \n ]。
5
"""
6
print(para_str)
7
# 这是一个多行字符串的实例
8
# 多行字符串可以使用制表符
9
# TAB ( )。
10
# 也可以使用换行符 [
11
# ]。
12
13
para_str = '''这是一个多行字符串的实例
14
多行字符串可以使用制表符
15
TAB ( \t )。
16
也可以使用换行符 [ \n ]。
17
'''
18
print(para_str)
19
# 这是一个多行字符串的实例
20
# 多行字符串可以使用制表符
21
# TAB ( )。
22
# 也可以使用换行符 [
23
# ]。
这是一个多行字符串的实例 多行字符串可以使用制表符 TAB ( )。 也可以使用换行符 [ ]。 这是一个多行字符串的实例 多行字符串可以使用制表符 TAB ( )。 也可以使用换行符 [ ]。
2. 字符串的切片与拼接
- 类似于元组具有不可修改性
- 从 0 开始 (和 Java 一样)
- 切片通常写成
start:end
这种形式,包括「start
索引」对应的元素,不包括「end
索引」对应的元素。 - 索引值可正可负,正索引从 0 开始,从左往右;负索引从 -1 开始,从右往左。使用负数索引时,会从最后一个元素开始计数。最后一个元素的位置编号是 -1。
【例子】
1
str1 = 'I Love LsgoGroup'
2
print(str1[:6]) # I Love
3
print(str1[5]) # e
4
print(str1[:6] + " 插入的字符串 " + str1[6:])
5
# I Love 插入的字符串 LsgoGroup
6
7
s = 'Python'
8
print(s) # Python
9
print(s[2:4]) # th
10
print(s[-5:-2]) # yth
11
print(s[2]) # t
12
print(s[-1]) # n
I Love e I Love 插入的字符串 LsgoGroup Python th yth t n
3. 字符串的常用内置方法
capitalize()
将字符串的第一个字符转换为大写。
【例子】
1
str2 = 'xiaoxie'
2
print(str2.capitalize()) # Xiaoxie
Xiaoxie
lower()
转换字符串中所有大写字符为小写。upper()
转换字符串中的小写字母为大写。swapcase()
将字符串中大写转换为小写,小写转换为大写。
【例子】
1
str2 = "DAXIExiaoxie"
2
print(str2.lower()) # daxiexiaoxie
3
print(str2.upper()) # DAXIEXIAOXIE
4
print(str2.swapcase()) # daxieXIAOXIE
daxiexiaoxie DAXIEXIAOXIE daxieXIAOXIE
count(str, beg= 0,end=len(string))
返回str
在 string 里面出现的次数,如果beg
或者end
指定则返回指定范围内str
出现的次数。
【例子】
1
str2 = "DAXIExiaoxie"
2
print(str2.count('xi')) # 2
2
endswith(suffix, beg=0, end=len(string))
检查字符串是否以指定子字符串suffix
结束,如果是,返回 True,否则返回 False。如果beg
和end
指定值,则在指定范围内检查。startswith(substr, beg=0,end=len(string))
检查字符串是否以指定子字符串substr
开头,如果是,返回 True,否则返回 False。如果beg
和end
指定值,则在指定范围内检查。
【例子】
1
str2 = "DAXIExiaoxie"
2
print(str2.endswith('ie')) # True
3
print(str2.endswith('xi')) # False
4
print(str2.startswith('Da')) # False
5
print(str2.startswith('DA')) # True
True False False True
find(str, beg=0, end=len(string))
检测str
是否包含在字符串中,如果指定范围beg
和end
,则检查是否包含在指定范围内,如果包含,返回开始的索引值,否则返回 -1。rfind(str, beg=0,end=len(string))
类似于find()
函数,不过是从右边开始查找。
【例子】
1
str2 = "DAXIExiaoxie"
2
print(str2.find('xi')) # 5
3
print(str2.find('ix')) # -1
4
print(str2.rfind('xi')) # 9
5 -1 9
isnumeric()
如果字符串中只包含数字字符,则返回 True,否则返回 False。
【例子】
1
str3 = '12345'
2
print(str3.isnumeric()) # True
3
str3 += 'a'
4
print(str3.isnumeric()) # False
True False
ljust(width[, fillchar])
返回一个原字符串左对齐,并使用fillchar
(默认空格)填充至长度width
的新字符串。rjust(width[, fillchar])
返回一个原字符串右对齐,并使用fillchar
(默认空格)填充至长度width
的新字符串。
【例子】
1
str4 = '1101'
2
print(str4.ljust(8, '0')) # 11010000
3
print(str4.rjust(8, '0')) # 00001101
11010000 00001101
lstrip([chars])
截掉字符串左边的空格或指定字符。rstrip([chars])
删除字符串末尾的空格或指定字符。strip([chars])
在字符串上执行lstrip()
和rstrip()
。
【例子】
1
str5 = ' I Love LsgoGroup '
2
print(str5.lstrip()) # 'I Love LsgoGroup '
3
print(str5.lstrip().strip('I')) # ' Love LsgoGroup '
4
print(str5.rstrip()) # ' I Love LsgoGroup'
5
print(str5.strip()) # 'I Love LsgoGroup'
6
print(str5.strip().strip('p')) # 'I Love LsgoGrou'
I Love LsgoGroup Love LsgoGroup I Love LsgoGroup I Love LsgoGroup I Love LsgoGrou
partition(sub)
找到子字符串sub,把字符串分为一个三元组(pre_sub,sub,fol_sub)
,如果字符串中不包含sub则返回('原字符串','','')
。rpartition(sub)
类似于partition()
方法,不过是从右边开始查找。
【例子】
1
str5 = ' I Love LsgoGroup '
2
print(str5.strip().partition('o')) # ('I L', 'o', 've LsgoGroup')
3
print(str5.strip().partition('m')) # ('I Love LsgoGroup', '', '')
4
print(str5.strip().rpartition('o')) # ('I Love LsgoGr', 'o', 'up')
('I L', 'o', 've LsgoGroup') ('I Love LsgoGroup', '', '') ('I Love LsgoGr', 'o', 'up')
replace(old, new [, max])
把 将字符串中的old
替换成new
,如果max
指定,则替换不超过max
次。
【例子】
1
str5 = ' I Love LsgoGroup '
2
print(str5.strip().replace('I', 'We')) # We Love LsgoGroup
We Love LsgoGroup
split(str="", num)
不带参数默认是以空格为分隔符切片字符串,如果num
参数有设置,则仅分隔num
个子字符串,返回切片后的子字符串拼接的列表。
【例子】
1
str5 = ' I Love LsgoGroup '
2
print(str5.strip().split()) # ['I', 'Love', 'LsgoGroup']
3
print(str5.strip().split('o')) # ['I L', 've Lsg', 'Gr', 'up']
['I', 'Love', 'LsgoGroup'] ['I L', 've Lsg', 'Gr', 'up']
【例子】
1
u = "www.baidu.com.cn"
2
# 使用默认分隔符
3
print(u.split()) # ['www.baidu.com.cn']
4
5
# 以"."为分隔符
6
print((u.split('.'))) # ['www', 'baidu', 'com', 'cn']
7
8
# 分割0次
9
print((u.split(".", 0))) # ['www.baidu.com.cn']
10
11
# 分割一次
12
print((u.split(".", 1))) # ['www', 'baidu.com.cn']
13
14
# 分割两次
15
print(u.split(".", 2)) # ['www', 'baidu', 'com.cn']
16
17
# 分割两次,并取序列为1的项
18
print((u.split(".", 2)[1])) # baidu
19
20
# 分割两次,并把分割后的三个部分保存到三个变量
21
u1, u2, u3 = u.split(".", 2)
22
print(u1) # www
23
print(u2) # baidu
24
print(u3) # com.cn
['www.baidu.com.cn'] ['www', 'baidu', 'com', 'cn'] ['www.baidu.com.cn'] ['www', 'baidu.com.cn'] ['www', 'baidu', 'com.cn'] baidu www baidu com.cn
【例子】去掉换行符
1
c = '''say
2
hello
3
baby'''
4
5
print(c)
6
# say
7
# hello
8
# baby
9
10
print(c.split('\n')) # ['say', 'hello', 'baby']
say hello baby ['say', 'hello', 'baby']
【例子】
1
string = "hello boy<[www.baidu.com]>byebye"
2
print(string.split('[')[1].split(']')[0]) # www.baidu.com
3
print(string.split('[')[1].split(']')[0].split('.')) # ['www', 'baidu', 'com']
www.baidu.com ['www', 'baidu', 'com']
splitlines([keepends])
按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数keepends
为 False,不包含换行符,如果为 True,则保留换行符。
【例子】
1
str6 = 'I \n Love \n LsgoGroup'
2
print(str6.splitlines()) # ['I ', ' Love ', ' LsgoGroup']
3
print(str6.splitlines(True)) # ['I \n', ' Love \n', ' LsgoGroup']
['I ', ' Love ', ' LsgoGroup'] ['I \n', ' Love \n', ' LsgoGroup']
maketrans(intab, outtab)
创建字符映射的转换表,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。translate(table, deletechars="")
根据参数table
给出的表,转换字符串的字符,要过滤掉的字符放到deletechars
参数中。
【例子】
1
str7 = 'this is string example....wow!!!'
2
intab = 'aeiou'
3
outtab = '12345'
4
trantab = str7.maketrans(intab, outtab)
5
print(trantab) # {97: 49, 111: 52, 117: 53, 101: 50, 105: 51}
6
print(str7.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!
{97: 49, 101: 50, 105: 51, 111: 52, 117: 53} th3s 3s str3ng 2x1mpl2....w4w!!!
4. 字符串格式化
format
格式化函数
【例子】
1
str8 = "{0} Love {1}".format('I', 'Lsgogroup') # 位置参数
2
print(str8) # I Love Lsgogroup
3
4
str8 = "{a} Love {b}".format(a='I', b='Lsgogroup') # 关键字参数
5
print(str8) # I Love Lsgogroup
6
7
str8 = "{0} Love {b}".format('I', b='Lsgogroup') # 位置参数要在关键字参数之前
8
print(str8) # I Love Lsgogroup
9
10
str8 = '{0:.2f}{1}'.format(27.658, 'GB') # 保留小数点后两位
11
print(str8) # 27.66GB
I Love Lsgogroup I Love Lsgogroup I Love Lsgogroup 27.66GB
- Python 字符串格式化符号
符 号 | 描述 |
---|---|
%c | 格式化字符及其ASCII码 |
%s | 格式化字符串,用str()方法处理对象 |
%r | 格式化字符串,用rper()方法处理对象 |
%d | 格式化整数 |
%o | 格式化无符号八进制数 |
%x | 格式化无符号十六进制数 |
%X | 格式化无符号十六进制数(大写) |
%f | 格式化浮点数字,可指定小数点后的精度 |
%e | 用科学计数法格式化浮点数 |
%E | 作用同%e,用科学计数法格式化浮点数 |
%g | 根据值的大小决定使用%f或%e |
%G | 作用同%g,根据值的大小决定使用%f或%E |
【例子】
1
print('%c' % 97) # a
2
print('%c %c %c' % (97, 98, 99)) # a b c
3
print('%d + %d = %d' % (4, 5, 9)) # 4 + 5 = 9
4
print("我叫 %s 今年 %d 岁!" % ('小明', 10)) # 我叫 小明 今年 10 岁!
5
print('%o' % 10) # 12
6
print('%x' % 10) # a
7
print('%X' % 10) # A
8
print('%f' % 27.658) # 27.658000
9
print('%e' % 27.658) # 2.765800e+01
10
print('%E' % 27.658) # 2.765800E+01
11
print('%g' % 27.658) # 27.658
12
text = "I am %d years old." % 22
13
print("I said: %s." % text) # I said: I am 22 years old..
14
print("I said: %r." % text) # I said: 'I am 22 years old.'
a a b c 4 + 5 = 9 我叫 小明 今年 10 岁! 12 a A 27.658000 2.765800e+01 2.765800E+01 27.658 I said: I am 22 years old.. I said: 'I am 22 years old.'.
- 格式化操作符辅助指令
符号 | 功能 |
---|---|
m.n | m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话) |
- | 用作左对齐 |
+ | 在正数前面显示加号( + ) |
# | 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X') |
0 | 显示的数字前面填充'0'而不是默认的空格 |
【例子】
1
print('%5.1f' % 27.658) # ' 27.7'
2
print('%.2e' % 27.658) # 2.77e+01
3
print('%10d' % 10) # ' 10'
4
print('%-10d' % 10) # '10 '
5
print('%+d' % 10) # +10
6
print('%#o' % 10) # 0o12
7
print('%#x' % 108) # 0x6c
8
print('%010d' % 5) # 0000000005
27.7 2.77e+01 10 10 +10 0o12 0x6c 0000000005
字典
1. 可变类型与不可变类型
- 序列是以连续的整数为索引,与此不同的是,字典以"关键字"为索引,关键字可以是任意不可变类型,通常用字符串或数值。
- 字典是 Python 唯一的一个 映射类型,字符串、元组、列表属于序列类型。
那么如何快速判断一个数据类型 X
是不是可变类型的呢?两种方法:
- 麻烦方法:用
id(X)
函数,对 X 进行某种操作,比较操作前后的id
,如果不一样,则X
不可变,如果一样,则X
可变。 - 便捷方法:用
hash(X)
,只要不报错,证明X
可被哈希,即不可变,反过来不可被哈希,即可变。
【例子】
1
i = 1
2
print(id(i)) # 140732167000896
3
i = i + 2
4
print(id(i)) # 140732167000960
5
6
l = [1, 2]
7
print(id(l)) # 4300825160
8
l.append('Python')
9
print(id(l)) # 4300825160
140731832701760 140731832701824 2131670369800 2131670369800
- 整数
i
在加 1 之后的id
和之前不一样,因此加完之后的这个i
(虽然名字没变),但不是加之前的那个i
了,因此整数是不可变类型。 - 列表
l
在附加'Python'
之后的id
和之前一样,因此列表是可变类型。
【例子】
1
print(hash('Name')) # 7047218704141848153
2
3
print(hash((1, 2, 'Python'))) # 1704535747474881831
4
5
print(hash([1, 2, 'Python']))
6
# TypeError: unhashable type: 'list'
7
8
-6668157630988609386 -1857436431894091236
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-6416367464f8> in <module>() 3 print(hash((1, 2, 'Python'))) # 1704535747474881831 4 ----> 5 print(hash([1, 2, 'Python'])) 6 # TypeError: unhashable type: 'list' TypeError: unhashable type: 'list'
1
print(hash({1, 2, 3}))
2
# TypeError: unhashable type: 'set'
- 数值、字符和元组 都能被哈希,因此它们是不可变类型。
- 列表、集合、字典不能被哈希,因此它是可变类型。
2. 字典的定义
字典 是无序的 键:值(key:value
)对集合,键必须是互不相同的(在同一个字典之内)。
dict
内部存放的顺序和key
放入的顺序是没有关系的。dict
查找和插入的速度极快,不会随着key
的增加而增加,但是需要占用大量的内存。
字典 定义语法为 {元素1, 元素2, ..., 元素n}
- 其中每一个元素是一个「键值对」-- 键:值 (
key:value
) - 关键点是「大括号 {}」,「逗号 ,」和「冒号 :」
- 大括号 -- 把所有元素绑在一起
- 逗号 -- 将每个键值对分开
- 冒号 -- 将键和值分开
3. 创建和访问字典
【例子】
1
brand = ['李宁', '耐克', '阿迪达斯']
2
slogan = ['一切皆有可能', 'Just do it', 'Impossible is nothing']
3
print('耐克的口号是:', slogan[brand.index('耐克')])
4
# 耐克的口号是: Just do it
5
6
dic = {'李宁': '一切皆有可能', '耐克': 'Just do it', '阿迪达斯': 'Impossible is nothing'}
7
print('耐克的口号是:', dic['耐克'])
8
# 耐克的口号是: Just do it
耐克的口号是: Just do it 耐克的口号是: Just do it
【例子】通过字符串或数值作为key
来创建字典。
1
dic1 = {1: 'one', 2: 'two', 3: 'three'}
2
print(dic1) # {1: 'one', 2: 'two', 3: 'three'}
3
print(dic1[1]) # one
4
print(dic1[4]) # KeyError: 4
5
{1: 'one', 2: 'two', 3: 'three'} one
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-2-bb8d02bd63a3> in <module>() 2 print(dic1) # {1: 'one', 2: 'two', 3: 'three'} 3 print(dic1[1]) # one ----> 4 print(dic1[4]) # KeyError: 4 KeyError: 4
1
dic2 = {'rice': 35, 'wheat': 101, 'corn': 67}
2
print(dic2) # {'wheat': 101, 'corn': 67, 'rice': 35}
3
print(dic2['rice']) # 35
{'rice': 35, 'wheat': 101, 'corn': 67} 35
注意:如果我们取的键在字典中不存在,会直接报错KeyError
。
【例子】通过元组作为key
来创建字典,但一般不这样使用。
1
dic = {(1, 2, 3): "Tom", "Age": 12, 3: [3, 5, 7]}
2
print(dic) # {(1, 2, 3): 'Tom', 'Age': 12, 3: [3, 5, 7]}
3
print(type(dic)) # <class 'dict'>
{(1, 2, 3): 'Tom', 'Age': 12, 3: [3, 5, 7]} <class 'dict'>
通过构造函数dict
来创建字典。
dict()
创建一个空的字典。
【例子】通过key
直接把数据放入字典中,但一个key
只能对应一个value
,多次对一个key
放入 value
,后面的值会把前面的值冲掉。
1
dic = dict()
2
dic['a'] = 1
3
dic['b'] = 2
4
dic['c'] = 3
5
6
print(dic)
7
# {'a': 1, 'b': 2, 'c': 3}
8
9
dic['a'] = 11
10
print(dic)
11
# {'a': 11, 'b': 2, 'c': 3}
12
13
dic['d'] = 4
14
print(dic)
15
# {'a': 11, 'b': 2, 'c': 3, 'd': 4}
{'a': 1, 'b': 2, 'c': 3} {'a': 11, 'b': 2, 'c': 3} {'a': 11, 'b': 2, 'c': 3, 'd': 4}
dict(mapping)
new dictionary initialized from a mapping object's (key, value) pairs
【例子】
1
dic1 = dict([('apple', 4139), ('peach', 4127), ('cherry', 4098)])
2
print(dic1) # {'cherry': 4098, 'apple': 4139, 'peach': 4127}
3
4
dic2 = dict((('apple', 4139), ('peach', 4127), ('cherry', 4098)))
5
print(dic2) # {'peach': 4127, 'cherry': 4098, 'apple': 4139}
{'apple': 4139, 'peach': 4127, 'cherry': 4098} {'apple': 4139, 'peach': 4127, 'cherry': 4098}
dict(**kwargs)
-> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
【例子】这种情况下,键只能为字符串类型,并且创建的时候字符串不能加引号,加上就会直接报语法错误。
1
dic = dict(name='Tom', age=10)
2
print(dic) # {'name': 'Tom', 'age': 10}
3
print(type(dic)) # <class 'dict'>
{'name': 'Tom', 'age': 10}