Python学习笔记之列表

列表简介

列表(list) 是Python基本数据类型之一,是一种可变序列、可以存储各种数据类型的集合,用 [] 表示一个列表的开始和结束,元素之间使用 , 分隔,列表中的每个元素都有对应下标,下标从0开始。

列表基本操作

列表 支持对集合中元素进行增加、查找、删除、排序、合并等操作,常用方法下表所示:

方法 功能
append 在列表尾部增加元素
insert 在列表指定位置插入元素
index 返回指定元素的下标
remove 从左到右遍历列表,删除第1个匹配的元素
clear 清空列表
sort 对列表中元素进行排序(列表中的元素需要相同类型)
count 统计元素在当前列表中的个数
extend 合并两个列表
reverse 永久反转列表
copy 浅拷贝列表
pop 删除并返回指定下标对应的元素

在Python IDLE中可以通过help函数查看对应说明。

增加&插入

>>> test_list = []           #声明空列表
>>> test_list.append(1)      #调用append方法追加元素1
>>> test_list
[1]
>>> test_list.insert(0, 2)   #调用insert方法在0下标位置插入元素2
>>> test_list
[2, 1]

删除&清空

>>> test_list = [2, 1]
>>> test_list.pop(0)        #删除0位置元素
2
>>> test_list
[1]

>>> test_list = [1, 2, 3]
>>> test_list.remove(3)    #删除列表中元素3
>>> test_list
[1, 2]
>>> test_list = [3, 1, 2, 3]
>>> test_list.remove(3)    #可以看出删除的是左边第1个3元素
>>> test_list
[1, 2, 3]
>>> test_list.remove(5)    #删除一个不存在的元素时,抛出ValueError
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    test_list.remove(5)
ValueError: list.remove(x): x not in list

>>> test_list = [1, 2, 3]
>>> test_list.clear()       #清空列表
>>> test_list
[]

复制

>>> test_list = [1, 2, 3]            #声明test_list列表为[1,2,3]
>>> test_list2 = test_list.copy()    #copy
>>> test_list2
[1, 2, 3]
>>> id(test_list)
45812360
>>> id(test_list2)
46149960

统计指定元素个数

>>> test_list = [1, 2, 3, 1]
>>> test_list.count(1)            #统计test_list列表中元素为1的个数
2
>>> test_list.count(4)
0

查找

>>> test_list = [1, 2, 3, 4]
>>> test_list.index(1)            #在test_list查找元素1的下标 
0
>>> test_list.index(5)            #如果列表中并没有这个元素,抛出ValueError
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    test_list.index(5)
ValueError: 5 is not in list

排序&反转

>>> test_list = [1, 3, 2, 4]
>>> test_list.sort()               #排序,默认正序
>>> test_list
[1, 2, 3, 4]
>>> test_list.sort(reverse=True)   #倒序
>>> test_list
[4, 3, 2, 1]

>>> test_list = [1, 3, 2, 'Python']
>>> test_list.sort()               #列表中元素不同类型会抛出TypeError
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    test_list.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list.reverse()            #列表反转 
>>> test_list
['iOS', 'Android', 'Java', 'Python']

合并两个列表

>>> test_list1 = [1, 2, 3]
>>> test_list2 = [4, 5, 6]
>>> test_list1.extend(test_list2)  #将test_list2的元素合并到test_list1中
>>> test_list1
[1, 2, 3, 4, 5, 6]
>>> test_list2
[4, 5, 6]

查看指定下标元素&遍历

>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list[0]       #查看下标为0的元素
'Python'
>>> test_list[1]       #查看下标为1的元素
'Java'
>>> test_list[2]       #查看下标为2的元素
'Android'
>>> test_list[3]       #查看下标为3的元素
'iOS'
>>> test_list[4]       #查看下标为4的元素,这里没有下标为4,抛出IndexError
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    test_list[4]
IndexError: list index out of range
------------------------------------------------------------
>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list[:]                #切片
['Python', 'Java', 'Android', 'iOS']
>>> for item in test_list:      #for循环
	print(item)	
Python
Java
Android
iOS

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章