列表
首次发布时间:2020/7/29
最后修改时间:2020/7/29
本系列文章基于 python3 版本
摘要
- 列表介绍
- 列表的循环遍历
- for 循环
- while 循环
- 列表的相关操作【增、删、改、查、排序】
- 列表的嵌套
- 思考题
列表介绍
什么是列表?
- 列表就是多个元素的集合,列表是一个容器
定义一个列表:
newList = [1, "a"]
列表的循环遍历:
- 使用 for 循环
newList = [1, 'a']
for i in newList:
print(i)
print('元素的类型是 : ' + str(type(i)))
- 输出结果:
1
元素的类型是 : <class 'int'>
a
元素的类型是 : <class 'str'>
- 使用 while 循环:
newList = [1, 'a']
i = 0
while i < len(newList):
print(newList[i])
print('元素的类型是 : ' + str(type(newList[i])))
i += 1
- 输出结果:
1
元素的类型是 : <class 'int'>
a
元素的类型是 : <class 'str'>
可以看到,当进行遍历时,使用 for 循环会方便很多
所以一般建议,对列表,字符串之类的遍历的时候,使用 for 循环
进行条件判断时,用 while 循环
列表的相关操作【增、删、改、查、排序】
增加元素【append、extend、insert】
- append【添加单个元素、添加一个列表】
firstList = [1, 'a']
firstList.append(2) # 添加单个元素
print(firstList)
newList = ['b', 3, 'c'] # 新建一个列表
firstList.append(newList) # 向列表 firstList 中添加列表 newList
print(firstList)
- 输出结果:
[1, 'a', 2]
[1, 'a', 2, ['b', 3, 'c']]
添加元素很好理解
而添加列表,把列表也看成一个元素即可,没什么特别的
实际上添加的列表就是一个元素
- extend【将另一个列表中的元素 逐一添加 到当前列表中】
firstList = [1, 'a', 2]
print(firstList)
newList = ['b', 3, 'c'] # 新建一个列表
firstList.extend(newList) # 将 newList 中的元素逐一添加到 firstList 中
print(firstList)
- 输出结果:
[1, 'a', 2]
[1, 'a', 2, 'b', 3, 'c']
- insert【insert(index, item):在指定位置(index) 前插入元素 item】
firstList = [1, 'a', 2]
print(firstList)
firstList.insert(1, 3) # 在下标为 1 的元素前面插入数字 3
print(firstList)
- 输出结果:
[1, 'a', 2]
[1, 3, 'a', 2]
修改元素
- 修改元素的时候,要通过下标来确定要修改的是哪个元素,然后才能进行修改
firstList = ['face', 'the', 'truth']
print(firstList)
firstList[0] = 'ignore'
print(firstList)
- 输出结果:
['face', 'the', 'truth']
['ignore', 'the', 'truth']
查找元素【in、not in、(index、count,上一篇已经讲过就不再赘述了)】
- 查找元素,就是看指定元素是否存在
- in:
firstList = ['face', 'the', 'truth']
str1 = "you"
str2 = "the"
if str1 in firstList:
print('字符 {} 存在'.format(str1));
else:
print('字符 {} 不存在'.format(str1));
if str2 in firstList:
print('字符 {} 存在'.format(str2));
else:
print('字符 {} 不存在'.format(str2));
- 输出结果:
字符 you 不存在
字符 the 存在
- not in
firstList = ['face', 'the', 'truth']
str1 = "you"
str2 = "the"
if str1 not in firstList:
print('字符 {} 不存在'.format(str1));
else:
print('字符 {} 存在'.format(str1));
if str2 not in firstList:
print('字符 {} 不存在'.format(str2));
else:
print('字符 {} 存在'.format(str2));
- 输出结果:
字符 you 不存在
字符 the 存在
其实 in 和 not in 用法大同小异,实际使用中看哪个更能表达清楚逻辑,就用哪个
删除元素【del、pop、remove】
- del:根据下标删除元素
- remove:根据元素的值进行删除
- pop:删除最后一个元素
newList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(newList)
del newList[2] # 删除下标为 2 的元素,即字符 'c'
print(newList)
newList.remove('f') # 删除字符 'f'
print(newList)
newList.pop() # 移除列表的最后一个元素,即字符 'g'
print(newList)
- 输出结果:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'd', 'e', 'f', 'g']
['a', 'b', 'd', 'e', 'g']
['a', 'b', 'd', 'e']
对于
remove('f')
经验证,假如列表中存在多个'f'
每调用一次 remove() 函数,只会删除当前列表最前面的'f'
(当 remove 其它的元素也是一样的道理)
排序【sort、reverse】
-
sort()
方法是将列表按特定顺序重新排列- 默认为由小到大
- 参数
reverse = True
可改为倒序,由大到小
-
reverse()
方法是将列表前后倒置 -
sort():
num1 = [5, 3 ,1, 2, 6, 4]
num1.sort()
print(num1)
newList = ['d', 'fc', 'a', 'e', 'b', 'fb', 'g', 'c', 'fa']
newList.sort()
print(newList)
num2 = [5, 3 ,1, 2, 6, 4]
num2.sort(reverse=True)
print(num2)
- 输出结果:
[1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd', 'e', 'fa', 'fb', 'fc', 'g']
[6, 5, 4, 3, 2, 1]
可以看到,对于字符,会根据字典序来排序
- reverse():
num = [5, 3 ,1, 2, 6, 4]
num.reverse()
print(num)
- 输出结果:
[4, 6, 2, 1, 3, 5]
reverse()
就是单纯地将列表前后倒置,并没有排序地功能,不要和 sort() 弄混了
列表的嵌套
- 一个列表中的元素,可以是单个字符、数字,也可以是一个列表
- 那么这时候就说这是列表的嵌套
list = [[a,b], [c,d], [e,f]]
- 列表内的元素可以是列表,可以是元组,也可以是字典,还可以是单个字符、数字等等
- 在以后的工作中,会经常的遇到各种各样结构的数据
思考
- 有 3 个货架,每个货架上有 4 个位置可以放物品
- 现在有 12 个物品需要摆放进去,要怎么实现随机摆放完所有物品?
import random
import functools
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
a = [[], [], []]
tmp = list
for i in range(3): # 表示 3 个货架
for j in range(4): # 表示货架上的 4 个位置
val = random.sample(tmp, 1) # 从 tmp 中取出其中一个物品,取出的 val 的格式是 list
val1 = functools.reduce(int, val) # 将 val 转换为 int
a[i].append(val1) # 将 val1 放进货架 i 上
tmp.remove(val1) # 将已经放入的物品从 tmp 列表中删除
print(a)
del tmp
- 输出结果:
[[2, 3, 7, 8], [1, 5, 6, 11], [4, 9, 10, 12]]
- 另一种方法:
import random
list_1 = [[], [], []]
list_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
for i in list_2:
index = random.randint(0, 2)
print(index) # 打印当前要放到哪个货架
if (len(list_1[index]) < 4) :
list_1[index].append(i)
elif(len(list_1[index - 1]) < 4):
list_1[index - 1].append(i)
elif (len(list_1[index - 2]) < 4):
list_1[index - 2].append(i)
print(list_1) # 打印最新的货架摆放情况
- 输出结果:
0
[[1], [], []]
1
[[1], [2], []]
1
[[1], [2, 3], []]
1
[[1], [2, 3, 4], []]
1
[[1], [2, 3, 4, 5], []]
1
[[1, 6], [2, 3, 4, 5], []]
2
[[1, 6], [2, 3, 4, 5], [7]]
0
[[1, 6, 8], [2, 3, 4, 5], [7]]
0
[[1, 6, 8, 9], [2, 3, 4, 5], [7]]
2
[[1, 6, 8, 9], [2, 3, 4, 5], [7, 10]]
1
[[1, 6, 8, 9], [2, 3, 4, 5], [7, 10, 11]]
0
[[1, 6, 8, 9], [2, 3, 4, 5], [7, 10, 11, 12]]
这种方法要说明一下,
因为有的情况下可能会出现list_1[-1].append()
或list_1[-2].append()
等情况
但是并不会报错的
因为其实list[-1]
表示 list 的最后一个元素
list[-2]
表示 list 的倒数第二个元素,以此类推
所以不会有越界的报错