1 列表
List (列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组
- 列表是可变数据类型
- 专门用于存储 一串 信息
- 列表用 [] 定义,数据 之间使用 , 分隔
- 列表的 下标 从 0 开始
2.常用操作
2.1 查询列表
2.1.1 index()函数
获得列表中数据第一次出现时的下标,可以指定查找范围
代码格式:
列表.index(数据,开始下标,结束下标)
name_list = ['tom','jack','hock']
print(name_list.index('tom')) # 0 下标为0
2.1.2 count()函数
统计数据在列表中出现的次数
count(数据)
name_list = ['tom','jack','hock']
print(name_list.count('tom')) # 1 次数为1
print(name_list.count('rom')) # 0 没有rom次数为0
2.1.3 len() 函数
返回列表长度
len(列表)
name_list = ['tom','jack','hock']
print(len(name_list)) # 3 数据个数为3
2.2 判断是否存在
in/not in 函数
返回布尔型数据True/False
in:存在返回 True;不存在返回 False。
not in : 存在返回 False;不存在返回 True。
name_list = ['tom','jack','hock']
i = 'tom' in name_list # True 存在tom 返回true
j = 'tom' not in name_list # False 存在tom返回false
print(i,j) # True False
2.3 增加函数
2.3.1 append(数据)
列表结尾追加数据
列表.append(数据)
- 注意:如果追加的数据是个列表那么,append()会把整个列表追加进去
name_list = ['tom','jack','hock']
name_list.append('faker')
print(name_list) # ['tom', 'jack', 'hock', 'faker']
name_list1 =['faker','theshy']
name_list.append(name_list1)
print(name_list) # ['tom', 'jack', 'hock', 'faker', ['faker', 'theshy']]
2.3.2 insert(数据,下标)
在指定位置插入数据
列表.insert(索引, 数据)
name_list = ['tom','jack','hock']
name_list.insert(1,'fast')
print(name_list) # ['tom', 'fast', 'jack', 'hock']
2.3.3 extend(列表2)
在结尾出增加数据/列表2,可以将数据的序列中每个数据/列表2 的中数据 逐一追加到列表
列表.extend(数据/列表2)
name_list = ['tom','jack','hock']
name_list1 =['faker','theshy']
name_list.extend('fast')
print(name_list) # ['tom', 'jack', 'hock', 'f', 'a', 's', 't']
name_list.extend(name_list1)
print(name_list) # ['tom', 'jack', 'hock', 'f', 'a', 's', 't', 'faker', 'theshy']
2.4 删除函数
2.4.1 del()函数
删除指定下标的数据
不指定下表表示删除整个列表
del 列表[下标]
# 不指定下标时表示删除整个列表
name_list = ['tom','jack','hock']
del name_list[1]
print(name_list) # ['tom', 'hock']
del name_list
print(name_list) # name 'name_list' is not defined 列表被删除无法读写
2.4.2 pop()函数
删除末尾数据/删除指定索引数据并且返回被删除的数据
列表.pop/列表.pop(索引)
name_list = ['tom','jack','hock']
name = name_list.pop()
print(name) # hock
print(name_list) # ['tom', 'jack']
name_list =['faker','pawn','clearlove','tom']
name_list.pop(2)
print(name_list) # ['faker', 'pawn', 'tom'] 'clearlove'下标为2被删除
2.4.3 remove()函数
删除第一个出现的指定数据
列表.remove[数据]
name_list = ['tom','facker','tom','jack']
name_list.remove('tom')
print(name_list) # ['facker', 'tom', 'jack']
2.4.4 clear()函数
清空列表
列表.clear
name_list = ['tom','facker','tom','jack']
name_list.clear()
print(name_list) #列表被清空了,输出空列表 []
2.5 修改函数
列表[下标] = 数据 # 对应下标的数据被修改为指定数据
name_list = ['tom','jack','hock']
name_list[1]='faker'
print(name_list) # ['tom', 'faker', 'hock']
排序函数
排序
列表.sort() 升序排序
列表.sort(reverse=True) 降序排序
列表.reverse() 逆序、反转
name_list = [1,2,3,6,5,4,3,7,8,9]
name_list.sort()
print(name_list) # [1, 2, 3, 3, 4, 5, 6, 7, 8, 9]
name_list.sort(reverse=True)
print(name_list) # [9, 8, 7, 6, 5, 4, 3, 3, 2, 1]
name_list.reverse()
print(name_list) # [1, 2, 3, 3, 4, 5, 6, 7, 8, 9] 逆序将原来的降序变为了升序
2.6 copy()函数
复制函数,将列表复制给其他列表
列表2 = 列表.copy()
name_list = ['tom','jack','hock']
name_list1 =name_list.copy()
print(name_list)
print(name_list1)
[‘tom’, ‘jack’, ‘hock’]
[‘tom’, ‘jack’, ‘hock’]
列表复制可以在不想对原列表进行修改时进行操作
2.7 循环函数
遍历 就是 从头到尾 依次 从 列表 中获取数据
- 在 循环体内部 针对 每一个元素,执行相同的操作
2.7.1while循环
name_list = ['tom','jack','hock']
i = 0
while i <len(name_list):
print(name_list[i],end='\t') # tom jack hock tom
i+=1
2.7.2 for循环
name_list = ['tom','jack','hock']
for j in name_list:
print(j) #jack
#hock
2.8 列表嵌套
在一个列表中的元素不再是单一的数据而是另一个列表。
列表 = [列表1,列表2,列表3,列表4...]
找到对应的下标就可以找到对应的数据
变量 = 列表[下标1][下标2]
name_list = [[1,2,3,4],[5,6,7,8,9,10],[11,22,44,33],[55,66,99,77]]
print(name_list[2][1]) # 22列表第一个下标找对应的列表 [11,22,44,33] 下标2 找到对应数据 22
3 练习
需求:
8个老师
3个办公室
8个老师随机分配到三个办公室
验证程序
import random
teacher =['a','b','c','d','e','f','g','h']
offices=[[],[],[]]
for name in teacher:
j = random.randint(0,2)
offices[j].append(name)
# print(offices)
#验证分配结果
i=1
for office in offices:
print(f'办公室{i}的人数为{len(office)},老师分别为:')
for name in office:
print(name,end=' ')
print('')
i+=1
办公室1的人数为1,老师分别为:
d
办公室2的人数为2,老师分别为:
a f
办公室3的人数为5,老师分别为:
b c e g h
办公室1的人数为0,老师分别为:
办公室2的人数为4,老师分别为:
a b c e
办公室3的人数为4,老师分别为:
d f g h