0
点赞
收藏
分享

微信扫一扫

Python list方法使用详解

吃面多放酱 2021-09-25 阅读 112
随笔

Python列表(list)的相关操作及方法

一、list列表

1.概述:
2.创建列表

使用:创建列表

#创建空列表
list1 = []
list1 = list()#格式化

#创建带有元素的列表
list2 = [10, 20, 30, 10]
print(list2)
#结果
[10, 20, 30, 10]

注意:在列表中元素的数据类型可以不同(灵活性)表中的元素类型可以是任意python中的基本数据类型或者是自定义的数据类型

list3 = [33, "good", True, 10.32]
print(list3)
#结果
[33, 'good', True, 10.32]
3.列表元素的访问
3.1 列表的取值
list4 = [22, 33, 12, 32, 45]
#下标从0开始,最大值为len(list4)-1
print(list4[0])

注意:当索引值大于len(list4)-1的时候,会出现以下错误:

print(list4[5])
IndexError: list index out of range

这个错误就是下标越界【下标超出了可表示的范围】

3.2 列表元素的替换
list4 = [22, 33, 12, 32, 45]
list4[0] = "hello"
print(list4[0])
4.列表操作
4.1 列表组合
list1 = [1, 2, 3]
list2 = ['hello', 'yes', 'no']
list3 = list1 + list2
print(list3)
#结果
[1, 2, 3, 'hello', 'yes', 'no']
4.2 列表重复
list1 = [1, 2, 3]
list2 = list1 * 3
print(list2)
#结果
[1, 2, 3, 1, 2, 3, 1, 2, 3]
4.3 判断元素是否在列表中
list1 = [1, 2, 3]
print(1 in list1)
#结果
True
4.4 列表截取
list1 = ["hello",2,True,False,3.14]
list2 = list1[:]
list3 = list1
print(list1[0:3:2])
print(list1[::-1])
print(list1[:3:-1])
print(list1[3::-1])
#结果
['hello', True]
[3.14, False, True, 2, 'hello']
[3.14]
[False, True, 2, 'hello']
4.5 二维列表
#创建二维列表,即列表中的元素还是列表
list1 = [[1, 2, 3],[2, 3, 4],[5, 4, 9]]

二维列表取值(访问)

list1 = [[1, 2, 3],[2, 3, 4],[5, 4, 9]]
print(list1[0][0])
5.列表的方法
5.1 list.append(元素/列表)
>>> list1 = [3, 4, 6]
>>> list1.append(6)
>>> print(list1)
[3, 4, 6, 6]
5.2 list.extend(列表)
>>> list1 = [1,2,3]
>>> list2 = [3, 4,5]
>>> list1.extend(list2)
>>> print(list1)
[1, 2, 3, 3, 4, 5]
5.3 list.insert(下标值, 元素/列表)
>>> list1 = [1,2,3]
>>> list1.insert(1,0)
>>> print(list1)
[1, 0, 2, 3]
>>> list1.insert(1,[2, 4, 8])
>>> print(list1)
[1, [2, 4, 8], 0, 2, 3]
5.4 list.pop(下标值)
>>> list1 = [1, [2, 4, 8], 0, 2, 3]
>>> list1.pop()
3
>>> print(list1)
[1, [2, 4, 8], 0, 2]
>>> list1.pop(2)
0
>>> print(list1)
[1, [2, 4, 8], 2]
5.5 list.remove(元素)
>>> list1 = [1, 2, 3]
>>> list1.remove(2)
>>> print(list1)
[1, 3]
5.6 list.clear()
>>> list1 = [1, 2, 3]
>>> list1.clear()
>>> print(list1)
[]
5.7 list.index(object[, start][, stop])
>>> list1 = [1, 2, 3]
>>> list1.index(2)
1
>>> list1.index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 4 is not in list

注意:若在列表中找不到这个元素,则会报错。

5.8 list.count(元素)
>>> list1 = [1, 2, 3, 1]
>>> list1.count(1)
2
5.9 len(list)
>>> list1 = [1, 2, 3, 1]
>>> len(list1)
4
5.10 max(list)
>>> list1 = [1, 2, 3, 1]
>>> max(list1)
3
5.11 min(list)
>>> list1 = [1, 2, 3, 1]
>>> min(list1)
1

注意:比较的是ASCII值

5.12 list.reverse()
list1 = ["hello1","good1","nice","good","hello"]
print(id(list1))
list1.reverse()
print(list1)
print(list1.reverse())
print(id(list1))
#结果
2158912823880
['hello', 'good', 'nice', 'good1', 'hello1']
None
2158912823880
5.13 list.sort()
>>> list1 = [1, 2, 3, 1]
>>> list1.sort()
>>> print(list1)
[1, 1, 2, 3]
5.14 赋值拷贝
list1 = [1,2,3,4,[1,2,3,4]]
list2 = list1
print(id(list1))
print(id(list2))
list2[-1] = True
print(list2)
print(list1)
#结果
1585735384392
1585735384392
[1, 2, 3, 4, True]
[1, 2, 3, 4, True]

注意:赋值拷贝为引用拷贝,类似于快捷方式

5.15 赋值拷贝
>>> list1 = [1, 2, 3, 1]
>>> list2 = list1.copy()
>>> print(list2)
[1, 2, 3, 1]
>>> print(id(list2))
4314525320
>>> print(id(list1))
4314524808

import copy
list1 = [1,2,3,4,[1,2,3,4]]
list3 = list1.copy()
list3[-1][-1] = True
print(id(list1))
print(id(list3))
print(list1)
print(list3)
#结果
#一维存储地址不同
1614254026312
1614254024904
#二维存储地址相同,伴随发生改变
[1, 2, 3, 4, [1, 2, 3, True]]
[1, 2, 3, 4, [1, 2, 3, True]]
5.16 深拷贝
list1 = [1,2,3,4,[1,2,3,4]]
list4 = copy.deepcopy(list1)
list4[-1][-1] ="hello"
print(id(list1))
print(id(list4))
print(list1)
print(list4)
#结果
#一维存储地址不同
2215608102408
2215608101128
#二维存储地址不同,不伴随发生改变
[1, 2, 3, 4, [1, 2, 3, 4]]
[1, 2, 3, 4, [1, 2, 3, 'hello']]
5.17 list(元组)
>>> list1 = list((1, 2, 3, 4))
>>> print(list1)
[1, 2, 3, 4]
6.列表的遍历
6.1 使用for循环遍历列表

说明:按照顺序获取列表中的每个元素,赋值给变量名,再执行语句,如此循环往复,直到取完列表中所有的元素为止

>>> list1 = ['hello', 78, '你好', 'good']
>>> for item in list1:
...     print(item)
... 
hello
78
你好
good
6.2 使用while循环遍历列表[使用下标循环]
6.3 同时遍历下标与元素
语法:

for 下标,变量 in enumerate(列表)
          语句
>>> list1 = ['hello', 78, '你好', 'good']
>>> for index,item in enumerate(list1):
...     print(index, item)
... 
0 hello
1 78
2 你好
3 good
enumerate()[枚举]函数用于一个可遍历的数据对象(如列表,元组或者字符串)组合为一个索引序列,同时列出数据与数据下标,一般使用在for循环中
enumerate(obj, [start =0])
obj:一个可迭代对象
start:下标起始位置

参考:https://blog.csdn.net/hu1258123819/article/details/91895869

举报

相关推荐

0 条评论