List列表
string、list 和 tuple 都属于 sequence(序列)
序列:Python 中最基本的数据结构,序列中的每个值都有对应的位置值,称之为索引
列表是写在方括号 [] 内、用逗号分隔开的元素列表,是Python 中使用最频繁的数据类型,可以完成大多数集合类的数据结构实现
列表中元素的类型可以不相同,可以嵌套列表
和字符串一样,列表可以被索引和截取
一、 创建列表
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可
代码示例:
list1 = ['a', 2 , 3.3, 'd']
list2 = ['e', '888']
print(list1) # 输出完整列表
print(list2) # 输出完整列表
'''
输出结果:
['a', 2, 3.3, 'd']
['e', '888']
'''
二、 嵌套列表
嵌套列表,即在列表里创建其它列表
代码示例:
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x)
print(x[0])
print(x[0][1])
'''
输出结果:
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
'''
三、 列表操作
列表脚本操作符
tips:+ 号用于组合列表,* 号用于重复列表
表达式 | 结果 | 描述 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 组合 |
[‘Hi!’] * 4 | [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] | 重复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print(x, end=" ") | 1 2 3 | 迭代 |
列表截取与拼接
表达式 | 描述 |
---|---|
L[i] | 读取索引为i元素 |
L[-i] | 从右侧开始读取倒数第i个元素 |
L[i:] | 输出从索引为i开始的所有元素 |
列表截取:
格式:
变量[头下标:尾下标]
变量[头下标:尾下标:步长]两种索引方式:
从左往右,以0开始
从右往左,以-1开始
列表连接:+
列表复制:* n(n代表复制的次数)
代码示例:
list1 = ['a', 2 , 3.3, 'd']
list2 = ['e', '888']
print(list1) # 输出完整列表
print(list1[0]) # 输出列表第一个元素
print(list1[1:3]) # 从第二个开始输出到第三个元素[1,3)
print(list1[2:]) # 输出从第三个元素开始的所有元素
print(list1[-3:-1]) #输出倒数第三个到倒数第二个元素[-3,-1)
print(list1 * 2) # 输出两次列表
print(list1 + list2) # 连接列表
print(list1[0:3:2]) #输出[1,4)中的元素,其中步长为2,即输出0,2元素
'''
输出结果:
['a', 2, 3.3, 'd']
a
[2, 3.3]
[3.3, 'd']
[2, 3.3]
['a', 2, 3.3, 'd', 'a', 2, 3.3, 'd']
['a', 2, 3.3, 'd', 'e', '888']
['a', 3.3]
'''
tips: 列表中的元素可以改变
代码示例:
list = ['a', 2 , 3.3, 'd','e','888']
print(list) # 输出完整列表
list[1] = 1
print(list)
list[2:4] = [3,'x']
print(list)
list[2:5] = [] #[2,5)的元素置为空
print(list)
'''
输出结果:
['a', 2, 3.3, 'd', 'e', '888']
['a', 1, 3.3, 'd', 'e', '888']
['a', 1, 3, 'x', 'e', '888']
['a', 1, '888']
'''
如果第三个参数为负数表示逆向读取,以下实例用于翻转字符串:
def reverseWords(input):
# 通过空格将字符串分隔符,把各个单词分隔为列表
inputWords = input.split(" ")
# 翻转字符串
# 假设列表 list = [1,2,3,4],
# list[0]=1, list[1]=2 ,而 -1 表示最后一个元素 list[-1]=4 ( 与 list[3]=4 一样)
# inputWords[-1::-1] 有三个参数
# 第一个参数 -1 表示最后一个元素
# 第二个参数为空,表示移动到列表末尾
# 第三个参数为步长,-1 表示逆向
inputWords=inputWords[-1::-1]
# 重新组合字符串
output = ' '.join(inputWords)
return output
if __name__ == "__main__":
input = 'I like runoob'
rw = reverseWords(input)
print(rw)
'''
输出结果:
runoob like I
'''
四、 更新列表
可以对列表的数据项进行修改或更新,你也可以使用 append() 方法来添加列表项
代码示例:
list = ['hello', 'world', 2021]
print("第三个元素为 : ", list[2])
list[1] = 'World!'
print("更新后的第二个元素为 : ", list[2])
list.append('2022')
print("更新后的列表 : ", list)
'''
输出结果:
第三个元素为 : 2021
更新后的第二个元素为 : 2021
更新后的列表 : ['hello', 'World!', 2021, '2022']
'''
五、 删除列表元素
使用del()
代码示例:
list = ['hello', 'world', 2021]
print ("原始列表 : ", list)
del list[2]
print ("删除第三个元素后 : ", list)
'''
输出结果:
原始列表 : ['hello', 'world', 2021]
删除第三个元素后 : ['hello', 'world']
'''
六、 内置函数
函数 | 描述 |
---|---|
len(list) | 返回列表元素个数 |
max(list) | 返回列表元素最大值 |
min(list) | 返回列表元素最小值 |
list(seq) | 将元组转换为列表 |
len(list)
返回列表元素个数
len(list)
- 参数
list : 要计算元素个数的列表 - 返回值
返回列表元素个数
代码示例:
list1 = ['hello', 'world', '!!!!']
print (len(list1))
list2=list(range(5)) # 创建一个 0-4 的列表
print (len(list2))
'''
输出结果:
3
5
'''
max(list)
返回列表元素最大值
max(list)
- 参数
list : 要返回最大值的列表 - 返回值
返回列表元素中的最大值
代码示例:
list1, list2 = ['hello', 'world', '2021'], [111, 222, 333]
print ("list1 最大元素值 : ", max(list1))
print ("list2 最大元素值 : ", max(list2))
'''
输出结果:
list1 最大元素值 : world
list2 最大元素值 : 333
'''
min(list)
返回列表元素最小值
min(list)
- 参数
list : 要返回最小值的列表 - 返回值
返回列表元素中的最小值
代码示例:
list1, list2 = ['hello', 'world', '2021','10211'], [111, 222, 333]
print ("list1 最小元素值 : ", min(list1))
print ("list2 最小元素值 : ", min(list2))
'''
输出结果:
list1 最大元素值 : 10211
list2 最大元素值 : 111
'''
list(seq)
将元组转换为列表
元组tuple | 列表list |
---|---|
值不能修改 | 值可以修改 |
存放于() | 存放于[] |
list( seq )
- 参数
seq : 要转换为列表的元组或字符串 - 返回值
返回列表
代码示例:
tuple = (666, 'hello', 'world', '!!!')
list1 = list(tuple)
print ("列表元素 : ", list1)
str="Hello World"
list2=list(str)
print ("列表元素 : ", list2)
'''
输出结果:
列表元素 : [666, 'hello', 'world', '!!!']
列表元素 : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
'''
七、 内置方法
方法 | 描述 |
---|---|
list.append(obj) | 在列表末尾添加新的对象 |
list.count(obj) | 统计某个元素在列表中出现的次数 |
list.extend(seq) | 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
list.index(obj) | 从列表中找出某个值第一个匹配项的索引位置 |
list.insert(index, obj) | 将对象插入列表 |
list.pop([index=-1]) | 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
list.remove(obj) | 移除列表中某个值的第一个匹配项 |
list.reverse() | 反向列表中元素 |
list.sort( key=None, reverse=False) | 对原列表进行排序 |
list.clear() | 清空列表 |
list.copy() | 复制列表 |
list.append(obj)
在列表末尾添加新的对象
list.append(obj)
- 参数
obj : 添加到列表末尾的对象 - 返回值
无返回值,但是会修改原来的列表
代码示例:
list = ['hello', 'world']
print("原列表 :",list)
list.append('!!!!')
print ("更新后的列表 : ", list)
'''
输出结果:
原列表 : ['hello', 'world']
更新后的列表 : ['hello', 'world', '!!!!']
'''
list.count(obj)
统计某个元素在列表中出现的次数
list.count(obj)
- 参数
obj : 列表中统计的对象 - 返回值
返回元素在列表中出现的次数
代码示例:
list = ['hello','hello', 'world']
print("原列表中hello的个数 :",list.count("hello"))
print("原列表中world的个数 :",list.count("world"))
'''
输出结果:
原列表中hello的个数 : 2
原列表中world的个数 : 1
'''
list.extend(seq)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
list.extend(seq)
- 参数
seq :元素列表,可以是列表、元组、集合、字典,若为字典,则仅会将键(key)作为元素依次添加至原列表的末尾 - 返回值
没有返回值,但会在已存在的列表中添加新的列表内容
代码示例:
list1 = ['hello','hello', 'world']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2) # 扩展列表
print(list1)
'''
输出结果:
['hello', 'hello', 'world', 0, 1, 2, 3, 4]
'''
list.index(obj)
从列表中找出某个值第一个匹配项的索引位置
list.index(x[, start[, end]])
- 参数
x :查找的对象
start :可选,查找的起始位置
end :可选,查找的结束位置 - 返回值
返回查找对象的索引位置,如果没有找到对象则抛出异常
代码示例:
list1 = ['hello','hello', 'world']
print(list1)
print(list1.index('hello'))
print(list1.index('world'))
'''
输出结果:
['hello', 'hello', 'world']
0
2
'''
list.insert(index, obj)
用于将指定对象插入列表的指定位置
list.insert(index, obj)
- 参数
index : 对象obj需要插入的索引位置。
obj : 要插入列表中的对象 - 返回值
没有返回值,但会在列表指定位置插入对象
代码示例:
list1 = ['hello','hello', 'world']
print(list1)
list1.insert(0,"666")
list1.insert(2,"2021")
print(list1)
'''
输出结果:
['hello', 'hello', 'world']
['666', 'hello', '2021', 'hello', 'world']
'''
list.pop([index=-1])
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list.pop([index=-1])
- 参数
index : 可选参数,要移除列表元素的索引值,不能超过列表总长度(默认为 index=-1,删除最后一个列表值) - 返回值
返回从列表中移除的元素对象
代码示例:
list1 = ['hello','hello', 'world',"!!!"]
print(list1)
print("list1.pop():",list1.pop())
print(list1)
print("list1.pop(1):",list1.pop())
print(list1)
'''
输出结果:
['hello', 'hello', 'world', '!!!']
list1.pop(): !!!
['hello', 'hello', 'world']
list1.pop(1): world
['hello', 'hello']
'''
list.remove(obj)
移除列表中某个值的第一个匹配项
list.remove(obj)
- 参数
obj : 列表中要移除的对象 - 返回值
没有返回值但是会移除列表中的某个值的第一个匹配项
代码示例:
list1 = ['hello','~','hello', 'world',"!!!"]
print(list1)
list1.remove('hello')
print("list1.remove('hello')后:",list1)
list1.remove('!!!')
print("list1.remove('!!!')后:",list1)
'''
输出结果:
['hello', '~', 'hello', 'world', '!!!']
list1.remove('hello')后: ['~', 'hello', 'world', '!!!']
list1.remove('!!!')后: ['~', 'hello', 'world']
'''
list.reverse()
反向排序列表中元素
list.reverse()
- 参数
无 - 返回值
没有返回值,但是会对列表的元素进行反向排序
代码示例:
list1 = ['hello','~','hello', 'world',"!!!"]
print(list1)
list1.reverse()
print("反向排序后:",list1)
'''
输出结果:
['hello', '~', 'hello', 'world', '!!!']
反向排序后: ['!!!', 'world', 'hello', '~', 'hello']
'''
list.sort( key=None, reverse=False)
对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数
list.sort( key=None, reverse=False)
- 参数
key :主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序
reverse : 排序规则,reverse = True 降序, reverse = False 升序(默认) - 返回值
没有返回值,但是会对列表的对象进行排序
代码示例:
list1 = ['666','333','555', '111',"999","888"]
print(list1)
list1.sort()
print("升序排序后:",list1)
list1.sort(reverse = True)
print("降序排序后:",list1)
'''
输出结果:
['666', '333', '555', '111', '999', '888']
升序排序后: ['111', '333', '555', '666', '888', '999']
降序排序后: ['999', '888', '666', '555', '333', '111']
'''
list.clear()
清空列表
list.clear()
- 参数
无 - 返回值
没有返回值,清空列表
代码示例:
list1 = ['666','333','555', '111',"999","888"]
print(list1)
list1.clear()
print("清空列表后后:",list1)
'''
输出结果:
['666', '333', '555', '111', '999', '888']
清空列表后后: []
'''
list.copy()
复制列表
list.copy()
- 参数
无 - 返回值
返回复制后的新列表
代码示例:
list1 = ['666','333','555', '111',"999","888"]
print("list1:",list1)
list2 = list1.copy()
print("copy list1 的list2:",list2)
'''
输出结果:
list1: ['666', '333', '555', '111', '999', '888']
copy list1 的list2: ['666', '333', '555', '111', '999', '888']
'''
Tuple元组
string、list 和 tuple 都属于 sequence(序列)
元组写在小括号 () 里,元素之间用逗号隔开
元组中的元素类型可以不相同
元组的元素不能被修改,但可以包含可变的对象,如list列表
元组不可变指的是元组所指向的内存中的内容不可变
一、 创建元组
在括号中添加元素,并使用逗号隔开即可
t1 = () # 构造0个元素的元组,空元组
t2 = (20,) # 构造1个元素的元组,需要在元素后添加逗号,否则括号会被当作运算符使用
t22 = (20) #括号被当作运算符使用,此时t22不是元组,而是整数
t3 = (1,2,3,4,5)
t4 = "a", "b", "c", "d"
print(t1)
print(t2)
print(t3)
print(t4)
print(type(t2))
print(type(t22))
'''
输出结果:
()
(20,)
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
<class 'tuple'>
<class 'int'>
'''
二、 元组操作
元组截取:
格式:
变量[头下标:尾下标]
变量[头下标:尾下标:步长]两种索引方式:
从左往右,以0开始
从右往左,以-1开始
元组连接:+
元组复制:* n(n代表复制的次数)
代码示例:
tuple1 = ('a', 2, 3.3, 'd', 5)
tuple2 = (6, '77')
print (tuple1) # 输出完整元组
print (tuple1[0]) # 输出元组的第一个元素
print (tuple1[1:3]) # 输出从第二个元素开始到第三个元素[1,3)
print (tuple1[2:]) # 输出从第三个元素开始的所有元素
print (tuple2 * 2) # 输出两次元组
print (tuple1 + tuple2) # 连接元组
'''
输出结果:
('a', 2, 3.3, 'd', 5)
a
(2, 3.3)
(3.3, 'd', 5)
(6, '77', 6, '77')
('a', 2, 3.3, 'd', 5, 6, '77')
'''
三、 修改元组
元组中的元素值不允许修改,但可以对元组进行连接组合
元组连接:+
代码示例:
t1 = ('a', 2, 3.3, 'd', 5)
t2 = (6, '77')
t3 = t1 + t2
# 以下修改元组元素操作是非法的
# t1[0] = 100
print (t3) # 连接元组
'''
输出结果:
('a', 2, 3.3, 'd', 5, 6, '77')
'''
四、 删除元组
元组中的元素值不允许删除,但可以使用del语句来删除整个元组
代码示例:
t1 = ('a', 2, 3.3, 'd', 5)
print(t1)
del t1
print("删除后的元组 t1 : ")
print(t1) #会抛出异常
'''
输出结果:
('a', 2, 3.3, 'd', 5)
删除后的元组 t1 :
Traceback (most recent call last): #抛出异常
File "D:\PythonProject\HelloWorld\HelloWorld.py", line 7, in <module>
print(t1)
NameError: name 't1' is not defined
'''
五、 运算符
符号 | 描述 | 代码示例 | 结果 |
---|---|---|---|
len() | 计算元素个数 | len((1, 2, 3)) | 3 |
+ | 连接 | (1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) |
*n | 复制n次 | (‘Hi!’,) * 4 | (‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’) |
in | 元素是否存在 | 3 in (1, 2, 3) | True |
for | 迭代 | for x in (1, 2, 3): print (x,) | 1 2 3 |
六、 内置函数
元组 t = (‘5’, ‘4’, ‘8’);
列表 list = [‘Hello’, ‘World’, ‘!!!’]
方法 | 描述 | 代码示例 | 结果 |
---|---|---|---|
len(tuple) | 计算元组元素个数 | len(t); | 3 |
max(tuple) | 返回元组中元素最大值 | max(t); | ‘8’ |
min(tuple) | 返回元组中元素最小值 | min(t); | ‘4’ |
tuple(iterable) | 将可迭代系列转换为元组 | t = tuple(list);print(t); | (‘Hello’, ‘World’, ‘!!!’) |
id(tuple) | 查看内存地址 | id(t) | 2660994964288 |
sorted() | 将元组排序,其返回值为一个列表(不改变元组) | t1 = sorted(t) | t1:[4,5,8] |
七、 内置方法
元组 t = (‘5’, ‘4’, ‘8’);
方法 | 描述 | 代码示例 | 结果 |
---|---|---|---|
count() | 计算指定元素在元组中出现的次数 | t.count( ‘4’) | 1 |
index() | 在元组中查找指定元素返回从左到右第一个索引,找不到则报错 | t.index(‘8’) | 2 |