numbers
[11, 1, 2, 3, 4, 5, 6, 7, 8, 9]
使用切片语法可以同时给多个元素赋值,通过使用切片赋值,可将切片替换为长度与其不同的序列,或者插入、删除元素:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
fruits[2:] = [‘lemon’, ‘watermelon’]
fruits
[‘apple’, ‘orange’, ‘lemon’, ‘watermelon’]
fruits[1:1] = [‘grape’]
fruits
[‘apple’, ‘grape’, ‘orange’, ‘lemon’, ‘watermelon’]
fruits[1:3] = []
fruits
[‘apple’, ‘lemon’, ‘watermelon’]
🔍 删除元素
从列表中删除元素可以使用 del
语句:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
del fruits[0]
fruits
[‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
del fruits[1:3]
fruits
[‘orange’, ‘strawberry’]
2 列表方法
方法是与对象紧密联系的函数,方法调用与函数类似,需要在方法名前加上了对象和句点:
object.method(arg)
🔍 index 方法
index
方法在列表中查找指定值第一次出现的索引:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
fruits.index(‘orange’)
1
fruits.index(‘lemon’)
Traceback (most recent call last):
File “”, line 1, in
ValueError: ‘lemon’ is not in list
查找单词“orange”时,返回其索引 4,但是当搜索列表中不存在的单词“lemon”时,会引发异常。
🔍 count 方法
count
方法用于统计指定元素在列表中的数量:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘apple’, ‘orange’, [‘apple’]]
fruits.count(‘apple’)
2
fruits.count([‘apple’])
1
🔍 append 方法
append
方法用于将一个对象添加到列表末尾:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
fruits.append(‘lemon’)
fruits
[‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’, ‘lemon’]
需要注意的是,与其他修改列表的方式类似,append
方法也是原地操作的,它不会返回修改后的新列表,而是直接在旧列表上进行修改。
🔍 extend 元素
extend
方法可以在一个列表末尾添加另一个列表,可使用一个列表来扩展原列表:
fruits_1 = [‘apple’, ‘orange’]
fruits_2 = [‘banana’, ‘pear’]
fruits_1.extend(fruits_2)
fruits_1
[‘apple’, ‘orange’, ‘banana’, ‘pear’]
与拼接运算符 +
不同的是,extend 方法是原地执行的,而 +
会返回得到的新列表,并非原地执行:
fruits_1 = [‘apple’, ‘orange’]
fruits_2 = [‘banana’, ‘pear’]
fruits = fruits_1 + fruits_2
fruits
[‘apple’, ‘orange’, ‘banana’, ‘pear’]
fruits_1
[‘apple’, ‘orange’]
🔍 insert 方法
insert
方法用于在列表的指定位置插入一个新元素:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
fruits.insert(2, ‘lemon’)
fruits
[‘apple’, ‘orange’, ‘lemon’, ‘banana’, ‘pear’, ‘strawberry’]
🔍 pop 方法
pop
方法用于在列表中删除一个指定位置(默认为最后一个)的元素,并返回这一元素:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘strawberry’]
fruits.pop()
‘strawberry’
fruits
[‘apple’, ‘orange’, ‘banana’, ‘pear’]
fruits.pop(1)
‘orange’
fruits
[‘apple’, ‘banana’, ‘pear’]
🔍 remove 方法
remove
方法用于移除在列表中首次出现的指定元素,但列表中之后出现的同样元素,并不会被删除:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘apple’, ‘strawberry’, ‘apple’]
fruits.remove(‘apple’)
fruits
[‘orange’, ‘banana’, ‘apple’, ‘strawberry’, ‘apple’]
🔍 sort 方法
sort
方法用于对列表原地排序,原地排序意味着对原来的列表按顺序排列,而不是返回排序后列表的副本:
x = [1, 3, 4, 9, 8, 2]
x.sort()
x
[1, 2, 3, 4, 8, 9]
由于 sort
修改原列表且不返回任何值,最终的结果列表是经过排序的。如果要获取排序后的列表的副本,而不修改原列表,需使用函数 sorted
:
x = [1, 3, 4, 9, 8, 2]
y = sorted(x)
y
[1, 2, 3, 4, 8, 9]
x
[1, 3, 4, 9, 8, 2]
sorted
方法同样可以用于其他序列,但返回值总是一个列表:
sorted(‘Hello world!’)
[’ ', ‘!’, ‘H’, ‘d’, ‘e’, ‘l’, ‘l’, ‘l’, ‘o’, ‘o’, ‘r’, ‘w’]
🔍 reverse 方法
reverse
方法按相反的顺序排列列表中的元素:
x.reverse()
x
[2, 8, 9, 4, 3, 1]
这类似于:
x = [1, 3, 4, 9, 8, 2]
x = x[::-1]
x
[2, 8, 9, 4, 3, 1]
类似的,如果要获取反向排列的列表副本,而不修改原列表,需要使用 reversed
函数:
x = [1, 3, 4, 9, 8, 2]
y = reversed(x)
x
[1, 3, 4, 9, 8, 2]
list(y)
[2, 8, 9, 4, 3, 1]
🔍 clear 方法
clear
方法就地清空列表的内容:
x = [1, 3, 4, 9, 8, 2]
x.clear()
x
[]
🔍 copy 方法
我们已经知道 Python
是基于值的管理方式,因此常规复制只是将另一个名称关联到列表,它们指向的是同一个列表,因此修改其中一个列表,另一个也会改变:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘strawberry’]
fruits_2 = fruits
fruits_2[1] = ‘lemon’
fruits
[‘apple’, ‘lemon’, ‘banana’, ‘strawberry’]
copy
方法用于复制列表,区别在于其使两个变量指向不同的列表,将列表 listB
关联到 listA
的副本:
listA = [‘apple’, ‘orange’, ‘banana’, ‘strawberry’]
listB = listA.copy()
listB[1] = ‘lemon’
listA
[‘apple’, ‘orange’, ‘banana’, ‘strawberry’]
3.2.3 字符串
Python
没有专门用于表示字符的类型,因此一个字符就是只包含一个元素的字符串。前面介绍了列表的方法,而字符串所拥有的方法要更多,其很多方法都是从模块 string
中“继承”而来的。这里并不会介绍所有字符串的方法,只会介绍一些对于之后数据结构和算法最有用的方法。
| 方法名 | 用法 | 解释 |
| — | — | — |
| lower | stringA.lower() | 将字符串中所有字母转换为大写 |
| upper | stringA.upper() | 将字符串中所有字母转换为小写 |
| count | stringA.count(item) | 返回字符串中 item 出现的次数 |
| center | stringA.center(width, fillchar) | 返回长度为 width 的字符串,原字符串居中,fillchar 为填充的字符,fillchar默认值为空格 |
| ljust | stringA.ljust(width, fillchar) | 返回长度为 width 的字符串,原字符串靠左,fillchar 为填充的字符,fillchar默认值为空格 |
| rjust | stringA.rjust(width, fillchar) | 返回长度为 width 的字符串,原字符串靠右,fillchar 为填充的字符,fillchar默认值为空格 |
| find | stringA.find(item) | 返回 item 第一次在字符串中的下标 |
| startswith | stringA.startswith(string) | 检查字符串是否以 string 开始,如果是,返回 True,否则返回 False. |
| endswith | stringA.endswith | 检查字符串是否以 string 结束,如果是,返回 True,否则返回 False. |
| replace | stringA.replace(old, new) | 将指定子串 old 都替换为另一个字符串 new,并返回替换后的结果 |
| split | stringA.split(char) | 以 str 为分隔符分割字符串 |
| join | string.join(listA) | 返回合并 listA 中元素得到的字符串,以 string 作为合并的分割符 |
🔍 lower 和 upper 方法
lower
方法返回字符串的小写版本,而 upper
方法返回字符串的大写版本:
string_1 = ‘Hello World!’
string_1.lower()
‘hello world!’
string_1.upper()
‘HELLO WORLD!’
🔍 count 方法
count
方法返回字符串中指定子串出现的次数:
string_2 = ‘data structure and algorithms’
string_2.count(‘a’)
4
string_2.count(‘th’)
1
🔍 center、ljust 和 rjust 方法
center
、ljust
和 rjust
方法返回一个字符串,原字符串居中 (center) /居左 (ljust) /居右 (rjust),使用指定字符填充新字符串,使其长度为指定的 width
:
string_3 = ‘Hello world!’
string_3.center(16, ‘*’)
‘Hello world!’
string_3.ljust(16, ‘*’)
‘Hello world!****’
string_3.rjust(16, ‘-’)
‘----Hello world!’
🔍 find 方法
find
方法在字符串中查找指定子串,如果找到,就返回第一个找到的子串的第一个字符的索引,否则返回 -1:
string_1 = ‘Explicit is better than implicit. Simple is better than complex.’
string_1.find(‘is’)
9
find
方法还支持使用可选参数指定查找的起点和终点,第二个参数指定搜索起点,第三个参数指定搜索终点:
string_1 = ‘Explicit is better than implicit. Simple is better than complex.’
string_1.find(‘is’, 10)
41
string_1.find(‘is’, 10, 40)
-1
🔍 replace 方法
replace
方法将指定子串都替换为另一个字符串,并返回替换后的结果:
string_1 = ‘Hello World!’
string_1.replace(‘World’, ‘Python’)
‘Hello Python!’
🔍 startswith 和 endswith 方法
startswith
方法检查字符串是否是以指定字符串开头,endswith
方法检查字符串是否是以指定字符串结尾,是则返回 True,否则返回 False,同时可以通过可选参数在指定范围内检查:
zen_of_python = ‘Beautiful is better than ugly. Explicit is better than implicit.’
zen_of_python.startswith(‘Beautiful’)
True
zen_of_python.startswith(‘Beautiful’, 1)
False
zen_of_python.endswith(‘ugly’, 0, 31)
False
zen_of_python.endswith(‘ugly’, 0, 29)
True
🔍 split 方法
split
方法用于使用指定分隔符将字符串拆分为序列:
zen_of_python = ‘Beautiful is better than ugly. Explicit is better than implicit.’
zen_of_python.split(‘is’)
['Beautiful ', ’ better than ugly. Explicit ‘, ’ better than implicit.’]
zen_of_python.split()
[‘Beautiful’, ‘is’, ‘better’, ‘than’, ‘ugly.’, ‘Explicit’, ‘is’, ‘better’, ‘than’, ‘implicit.’]
如果没有指定分隔符,默认情况下使用单个或多个连续的空白字符作为分隔符。
🔍 join 方法
与 split
方法相反,join
方法用于使用指定分割符合并序列的元素,并返回合并后的字符串:
‘’.join([‘1’,‘2’,‘3’])
‘123’
fruits = [‘apple’, ‘orange’, ‘lemon’, ‘banana’]
‘–’.join(fruits)
‘apple–orange–lemon–banana’
‘’.join([1,2,3])
Traceback (most recent call last):
File “”, line 1, in
TypeError: sequence item 0: expected str instance, int found
需要注意的是,所合并序列的元素必须都是字符串。
3.2.4 元组
元组与列表非常相似,区别在于,元组和字符串一样是不可修改的。元组的所有元素包含在圆括号内并且以逗号分隔。元组允许使用适用于通用序列的任一操作:
tuple_1 = tuple()
tuple_1
()
tuple_1 = tuple([1,2,3])
len(tuple_1)
3
tuple_1[0:1]
(1,)
可以使用 tuple
函数创建元素,如果尝试改变元组中的元素,就会导致异常:
tuple_1 = (1,2,3)
tuple_1[1] = 4
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘tuple’ object does not support item assignment
3.2.5 集合
集合 (set
) 是无序的不重复元素序列,形式为以花括号包含、以逗号分隔的一系列值,可以使用大括号 {}
或者 set()
函数创建集合(创建一个空集合必须用 set()
而不能使用 {}
,因为 {}
是用来创建一个空字典):
setA = {‘apple’, ‘banana’, ‘lemon’}
setA
{‘lemon’, ‘banana’, ‘apple’}
setB = set([‘apple’, ‘banana’, ‘lemon’])
setB
{‘lemon’, ‘banana’, ‘apple’}
1 基本集合运算
集合支持以下运算:
| 运算名 | 运算符 | 解释 |
| — | — | — |
| 成员 | in | 查询集合中是否包含指定元素 |
| 成员 | not in | 查询集合中是否不包含指定元素 |
| 长度 | len | 获取集合中元素个数 |
| | | setA | setB | 返回 setA 和 setB 的并集 |
| & | setA & setB | 返回 setA 和 setB 的交集 |
| - | setA - setB | 返回 setA 和 setB 的差集 |
| <= | setA <= setB | 查询 setA 是否为 setB 的子集 |
setA = {‘apple’, ‘banana’, ‘lemon’}
len(setA)
3
‘apple’ in setA
True
setB = {‘apple’, ‘banana’, ‘orange’}
setA | setB
{‘banana’, ‘apple’, ‘lemon’, ‘orange’}
setA & setB
{‘apple’, ‘banana’}
setA - setB
{‘lemon’}
setB - setA
{‘orange’}
setA <= setB
False
setC = {‘apple’, ‘banana’}
setC <= setA
True
2 基本集合方法
集合同样支持一系列方法,其中一些方法具有相对应的运算符,如 union
、intersection
、difference
等:
| 方法名 | 用法 | 解释 |
| — | — | — |
| union | setA.union(setB) | 返回 setA 和 setB 的并集 |
| intersection | setA.intersection(setB) | 返回 setA 和 setB 的交集 |
| difference | setA.difference(setB) | 返回 setA 和 setB 的差集 |
| issubset | setA.issubset(setB) | 查询 setA 是否为 setB 的子集 |
| add | setA.add(item) | 向 setA 添加元素 item |
| remove | setA.remove(item) | 从集合 setA 中移除 item |
| pop | setA.pop() | 随机移除 setA 中的一个元素 |
| clear | setA.clear() | 清空 setA 中的所有元素 |
🔍 union、intersection、difference 与 issubset 方法
union
、intersection
、difference
与 issubset
方法分别对应于集合运算符 |
、&
、-
和 <=
:
setA = {‘lemon’, ‘banana’, ‘apple’}
setB = {‘banana’, ‘apple’, ‘orange’}
setA.union(setB)
{‘banana’, ‘apple’, ‘lemon’, ‘orange’}
setA.intersection(setB)
{‘apple’, ‘banana’}
setA.difference(setB)
{‘lemon’}
setA.issubset(setB)
False
🔍 add 方法
add
方法用于向集合中添加元素:
setA = {‘lemon’, ‘banana’, ‘apple’}
setA.add(‘orange’)
setA
{‘lemon’, ‘banana’, ‘apple’, ‘orange’}
setA.add(‘apple’)
setA
{‘lemon’, ‘banana’, ‘apple’, ‘orange’}
🔍 remove 方法
remove
方法用于从集合中移除指定元素:
setA = {‘lemon’, ‘banana’, ‘apple’, ‘orange’}
setA.remove(‘apple’)
setA
{‘lemon’, ‘banana’, ‘orange’}
setA.remove(‘apple’)
Traceback (most recent call last):
File “”, line 1, in
KeyError: ‘apple’
从上示例可以看出,使用 remove
方法时当元素不存在,则会引发错误。
🔍 pop 方法
pop
方法用于从集合中移除随机元素,并且返回值为移除的元素:
setA = {‘banana’, ‘apple’, ‘lemon’, ‘orange’}
setA.pop()
‘banana’
setA
{‘apple’, ‘lemon’, ‘orange’}
🔍 clear 方法
clear
方法用于清空集合:
setA
{‘apple’, ‘lemon’, ‘orange’}
setA.clear()
setA
set()
3.2.6 字典
字典由键及其相应的值组成,每个键-值
对称为一项 (item
)。在以下示例中,键为水果名,而值为价格。整个字典放在花括号内,每个键与其值之间都用冒号 (:
) 分隔,项之间用逗号分隔,空字典用 {}
表示:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
fruits
{‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
需要注意的是,字典并不是根据键来进行有序维护的,键的位置是由散列来决定的。
1 基本字典运算
字典的基本运算符如下表所示:
| 运算名 | 运算符 | 解释 |
| — | — | — |
| [] | dictA[key] | 返回与键 key 相关联的值,如果没有则报错 |
| in | key in dictA | 如果字典中包含键 key,返回 True,否则返回 False |
| not in | key not in dictA | 如果字典中不包含键 key,返回 True,否则返回 False |
| del | del dictA[key] | 从字典dictA中删除 key 对应的键-值对 |
🔍 创建字典
可使用函数 dict
从其他映射或键-值对序列创建字典:
fruits = [(‘apple’, 5.0), (‘orange’, 3.5)]
d_fruits = dict(fruits)
d_fruits
{‘apple’: 5.0, ‘orange’: 3.5}
同样也可以通过实参来调用 dict
函数:
d_fruit = dict(apple=5.0,orange=3.5)
d_fruit
{‘apple’: 5.0, ‘orange’: 3.5}
字典中的键可以是任何不可变的类型,如实数、字符串或元组,但不能是列表,因为列表是可变类型。
🔍 访问字典中的值
访问字典的语法与访问序列的语法十分相似,只不过是使用键来访问,而不是下标:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
fruits[‘apple’]
5.0
字典中同样可以使用成员运算符,但表达式 key in dictA
或 key not in dictA
(其中 dictA
是一个字典)查找的是键而不是值:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
5.0 in fruits
False
‘apple’ in fruits
True
len(dictA)
返回字典 dictA
中包含的项(键-值对)数:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
len(fruits)
3
🔍 修改字典元素
修改字典中的元素,可以使用 d[key] = value
将值 value
关联到键 key
上,如果字典中原本没有的键 key
,将会在字典中创建一个新项:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
fruits[‘apple’] = 5.5
fruits
{‘apple’: 5.5, ‘orange’: 3.5, ‘banana’: 2}
fruits[‘lemon’] = 6.0
fruits
{‘apple’: 5.5, ‘orange’: 3.5, ‘banana’: 2, ‘lemon’: 6.0}
🔍 删除字典元素
del dictA[key]
用于删除键为 key
的项:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
del fruits[‘apple’]
fruits
{‘orange’: 3.5, ‘banana’: 2}
2 基本字典方法
与其他内置类型一样,字典也有其特有方法:
| 方法名 | 用法 | 解释 |
| — | — | — |
| get | dictA.get(key, alt) | 返回字典 dictA 中 key 对应的值,如果没有则返回 alt,如果不指定 alt 则返回值为 None |
| keys | dictA.keys() | 返回字典 dictA 中所有键 |
| values | dictA.values() | 返回字典 dictA 中所有值 |
| items | dictA.items() | 返回字典 dictA 中所有键–值对 |
🔍 get 方法
get
方法的存在,可以弥补访问字典中没有的项时引发错误的问题:
fruits = {‘apple’: 5.0, ‘orange’: 3.5, ‘banana’: 2}
fruits[‘lemon’]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!