python基础——列表详解

花间影清欢课程 2024-03-29 23:40:31
一、简述

列表是Python最常用的数据类型,格式是方括号[]中包含以逗号分隔的字符或者数字。列表中的每个元素都有一个位置(称为下标或者索引),从0开始。

二、创建列表>>> list1 = [1,2,3,4,5] # 推荐或者>>> list2 = list([5,4,3,2,1])>>> list3 = [1, 'a', func1]三、列表常操作

列表详解思维导图

1. 获取列表中的值

>>> list1[1, 2, 3, 4, 5]>>> list1[1]2

2. append 追加

>>> list1.append(6)>>> list1[1, 2, 3, 4, 5, 6]

3. count 统计列表中的元素数量

>>> list1.count(6)1

4. extend 扩展列表,通常用于把一个列表扩容到另一个列表中

>>> list1[1, 2, 3, 4, 5, 6]>>> list1.extend(list1)>>> list1[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

5. index 查看元素索引,返回的是第一个要查找元素的位置下标,找不到回报ValueError异常

>>> list1.index(5)4

6. insert 插入元素,需要指定一个要插入的下标位置

>>> list1.insert(1,9)>>> list1[1, 9, 2, 3, 4, 5, 6]

7. pop 删除元素,可以指定下标,默认删除最后一个元素。而且pop比较特别的是,删除后还可以返回删除了哪个元素

>>> list1[1, 9, 2, 3, 4, 5, 6]>>> list.pop()1>>> list1[1, 9, 2, 3, 4, 5]>>> list1.pop(1)9>>> list1[1, 2, 3, 4, 5]>>> p = list1.pop(4)>>> p5

8. remove 删除匹配到的第一个元素,必须指定一个元素名称,和pop不一样

>>> list1.remove(2)>>> list1[1, 3, 4, 5, 6]

9. sort 排序元素。 python2中如果元素有数字和字符串,会按照ASCII码来排序,但在Python3中数字和字符串排序会报错

Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> l1 = ['c','b','d','z',1,3,2,5,3]>>> l1.sort()>>> l1[1, 2, 3, 3, 5, 'b', 'c', 'd', 'z']Python3:>>> l1 = ['c','b','d','z',1,3,2,5,3]>>> l1.sort()Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> l1.sort()TypeError: '<' not supported between instances of 'int' and 'str'>>> l2 = [3,5,2,1,2,34,23,5]>>> l2.sort()>>> l2[1, 2, 2, 3, 5, 5, 23, 34]

10. reverse 反转

>>> l2.reverse()>>> l2[34, 23, 5, 5, 3, 2, 2, 1]>>> l1.reverse()>>> l1[3, 5, 2, 3, 1, 'z', 'd', 'c', 'b']

11. 删除元素 del,也可以使用列表切片的形式来一次删除多个元素

>>> l1[3, 5, 2, 3, 1, 'z', 'd', 'c', 'b']>>> del l1[1]>>> l1[3, 2, 3, 1, 'z', 'd', 'c', 'b']>>> del l1[4]>>> l1[3, 2, 3, 1, 'd', 'c', 'b']>>> del l1[1:3]>>> l1[3, 1, 'd', 'c', 'b']

12. 修改元素

>>> l1[3, 1, 'd', 'c', 'b']>>> l1[2] = 'D'>>> l1[3, 1, 'D', 'c', 'b']

13. 列表元素个数 len

>>> len(l1)5

14. max 返回列表中最大的元素,也是存在python3中不能字符串和数字比较的问题

Python3:>>> max(l1)Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> max(l1)TypeError: '>' not supported between instances of 'int' and 'str'>>> max(l2)34Python2:>>> l1 = ['a','z',1,3,33]>>> max(l1)'z'

15. 返回最小元素 min,同max

Python2>>> min(l1)1Python3>>> min(l1)Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> min(l1)TypeError: '>' not supported between instances of 'int' and 'str'

16. 列表切片

>>> l1[3, 1, 'D', 'c', 'b']# 列出前三个元素>>> l1[:3][3, 1, 'D']# 列出最后一个元素>>> l1[-1:]['b']# 列出从第一个元素至倒数第二个元素>>> l1[:-1][3, 1, 'D', 'c']# 隔一个列出一个元素,这里在第三段设定一个步长即可;>>> l1[::2][3, 'D', 'b']

17. copy 复制列表,但这里是浅copy,也就是只copy一层元素,如果列表中嵌套了列表或者字典,这里的copy只是copy了第一层,嵌套的元素只是copy了一个内存指针,如果嵌套的元素变化,copy过来的元素也是会变的

# 定义一个l3列表,里面嵌套两层列表>>> l3 = [1,2,3,['a','b','c',['A','B','D']],4,5]# l4从l3 copy>>> l4 = l3.copy()# 列出l3元素>>> l3[1, 2, 3, ['a', 'b', 'c', ['A', 'B', 'D']], 4, 5]# 列出l4元素>>> l4[1, 2, 3, ['a', 'b', 'c', ['A', 'B', 'D']], 4, 5]# 修改l3嵌套的列表值>>> l3[3][1] = 'X'>>> l3[3][2] = 'Y'>>> l3[3][3] = 'Z'# 而后查看l3和l4的元素,发现l3改了之后l4也跟着改了>>> l3[1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]>>> l4[1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]

18. 如果要深度拷贝,可以使用Python标准模块copy的deepcopy方法

>>> import copy>>> l3 = [1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]>>> l5 = copy.deepcopy(l3)>>> >>> >>> l5[1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]>>> l3[1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]>>> l3[3][0] = 'A'>>> l3[1, 2, 3, ['A', 'X', 'Y', 'Z'], 4, 5]>>> l5[1, 2, 3, ['a', 'X', 'Y', 'Z'], 4, 5]
0 阅读:0