列表(list)
1.切片 - 获取部分元素(切片结果是列表)
1.1语法: 列表[开始下标:结束下标:步长]
1.2说明:
1.2.1开始和结束下标确定切片范围([开始下标,结束下标))
1.2.2步长决定切片方向(开始到结束的方向要和步长的方向保持一致)
1.2.3步长为正就是正向,否则就是反向
1.2.4同时步长还决定是一个一个取还是跳着取
1.2.5从负的开始下标到正的结束下标时必须要确定步长的方向
1.3省略步长,默认步长就是1
games = ['王者荣耀','和平精英','超级玛丽','穿越火线','英雄联盟']
print(games[-1:-5:-1])
print(games[::])
print(games[0:-1])
print(games[0:-1:1])
print(games[0:-1:2])
print(games[-1:0])
print(games[-1:0:1])
print(games[-1:0:-1])
print(games[-1:0:-2])
1.4省略开始下标 列表[:结束下标:步长] - 列表[:结束下标]
1.4.1省略开始下标,步长为正,从第一元素往后取,步长为负,从最后一个元往前取
games = ['王者荣耀','和平精英','超级玛丽','穿越火线','英雄联盟']
print(games[:3])
print(games[:3:-1])
1.5省略结束下标 列表[开始下标::步长] - 列表[开始下标:]
1.5.1省略结束下标,如果步长为正,从开始下标到最后一个元素,步长为负,从开始下标取到第一个元素
games = ['王者荣耀','和平精英','超级玛丽','穿越火线','英雄联盟']
print(games[1::])
print(games[1::-1])
print(games[-1::-1])
1.6同时省略开始下标和结束下标 列表[::步长] - 列表[:]
1.6.1用步长确定方向
games = ['王者荣耀','和平精英','超级玛丽','穿越火线','英雄联盟']
print(games[:])
print(games[::-1])
2.删 - 删除列表元素(让列表元素个数减少)
2.1 删除一个元素
2.1.1 del 列表[下标] - 删除列表中指定下标对应的元素
下标不能越界
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑', '康熙王朝', '破产姐妹', '亮剑']
del teleplays[3]
print(teleplays)
2.1.2 列表.remove(元素) 删除列表中的指定元素
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑', '康熙王朝', '破产姐妹', '亮剑']
teleplays.remove('回家的诱惑')
print(teleplays)
如果元素不存在,报错值错误
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑', '康熙王朝', '破产姐妹', '亮剑']
teleplays.remove('aaa')
如果存在多个相同的值时,只会删除第一个
aa = [10,20,30,10,10,50]
aa.remove(10)
print(aa)
2.1.3 pop方法
列表.pop() - 取出列表最后一个元素,可以用来
列表.pop(下标) - 取出列表指定下标对应的元素
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑', '康熙王朝']
aa = teleplays.pop()
print(teleplays)
print(aa)
bb = teleplays.pop(3)
print(teleplays)
print(bb)
2.1.4 列表.clear() - 将列表清空
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑']
teleplays.clear()
print(teleplays)
3.改 - 修改元素的值
语法: 列表[下标] = 值 - 将指定下标对应的元素修改成指定的值
teleplays = ['海上繁花','你微笑时很美','九千米爱情','回家的诱惑']
print(teleplays)
teleplays[3] = '庆余年'
print(teleplays)
练习1:将低于60分的成绩改成0
方法1
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
for index in range(len(scores)):
if scores[index] < 60:
scores[index] = 0
print(scores)
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
for item in scores:
if item < 60:
scores[scores.index(item)] = 0
print(scores)
方法2
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
for index,item in enumerate(scores):
if item<60:
scores[index] = 0
print(scores)
方法3
在保留元数据的情况,产生新列表进行修改
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
new_scores = []
for item in scores:
if item < 60:
new_scores.append(0)
continue
new_scores.append(item)
print(new_scores)
练习2:删除列表中低于60分的成绩
方法1
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
count = 0
for index in range(len(scores)):
if scores[index] < 60:
scores[index] = 0
count+=1
while count:
scores.remove(0)
count-=1
print(scores)
方法2
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
for item in scores[::-1]:
if item<60:
scores.remove(item)
print(scores)
方法3
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
index = 0
while index<len(scores):
if scores[index]<60:
scores.pop(index)
else:
index+=1
print(scores)
4.列表的运算
4.1 数学运算:+,*
4.1.1 列表1+列表2 - 将列表1和列表2合并为一个新的列表
4.1.2 列表1 * N 将N个列表1合并为一个新列表
print([1,2,3]+[4,5,6])
print([1,2,3,4]*3)
4.2比较运算符:==,!=,>,<,>=,<=
4.2.1 ==,!= 必须要元素个数相等和对应元素相同才相等
print([1,2,3] == [1,2,3])
print([1,2,3] == [1,3,2])
4.2.2 列表1 > (>=,<,<=) 列表 不看元素个数
原理:比较的是第一对不相等的元素大小(两个列表下标相等为一对,下标从0开始)
print([1,20,3,40]>[10,1])
4.3 in 或 not in
功能:判断列表中是否存在或不存在指定元素
print(10 in [10,20,30,])
print([10,20] in [10,20,30])
print([10,20] in [10,[10,20],20])
实例
判断分数值是不是100或者0或者60
score = 60
if score==100 or score==0 or score ==60:
print("特殊值")
score = 60
if score in [0,60,100]:
print("特殊值")
4.4列表相关方法:count,index,reverse,sort,copy
4.4.1 count方法 列表.count(元素) - 统计指定的元素的个数
元素不存在返回0
nums = [10,20,30,4,10,20,10,20]
print(nums.count(10))
4.4.2 index方法 列表.index(元素) - 获取指定元素第一次在列表出现的下标
元素不存在会报错
nums = [10,20,30,4,10,20,10,20]
print(nums.index(20))
4.4.3 reverse方法 列表.reverse() - 将列表翻转
nums = [10,20,30,4,10,20,10,20]
nums.reverse()
print(nums)
4.4.4 sort方法 列表.sort() - 将列表从小到大排序
nums = [10,20,30,4,10,20,10,20]
nums.sort()
print(nums)
列表.sort(reverse = True) - 将列表从大到小排序
nums = [10,20,30,4,10,20,10,20]
nums.sort(reverse=True)
print(nums)
4.4.5 copy方法 列表.copy() - 复制列表产生一个相同的新列表
list1 = [1,2,3,4,5,6]
list2 = list1.copy()
print(list2)
作业题
1.已知一个数字列表,求列表中心元素。
list1 = [1,2,3,4,5,6]
if len(list1) % 2 ==0:
print(list1[len(list1)//2-1],list1[len(list1)//2])
else:
print(list1[len(list1)//2])
2.已知一个数字列表,求所有元素和。
list1 = [1,2,3,4,5,6]
sum1 = 0
for item in list1:
sum1+=item
print(sum1)
3.已知一个数字列表,输出所有奇数下标元素。
list1 = [1,2,3,4,5,6]
for item in list1:
if list1.index(item) % 2 !=0:
print(item)
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
list1 = [1,3,2,5,4,6]
for item in list1:
if item % 2 !=0:
print(item)
5.已知一个数字列表,将所有元素乘二。
list1 = [1,2,3,4,5,6]
for index in range(len(list1)):
list1[index] *=2
print(list1)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
names = ['张三', '李四', '大黄', '大黄', '张三',
'张三', '张三','王二','王五','李四']
new_names=[]
new_names.append(names[0])
for index in range(len(names)):
if names[index] not in new_names:
new_names.append(names[index])
print(new_names)
7.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
scores = [6,8,8,9,7,6,5,4,]
max = scores[0]
min = scores[0]
avg = 0
for item in scores:
if item>max:
max = item
if item<min:
min = item
scores.remove(max)
scores.remove(min)
for item in scores:
avg += item
print("节目平均分数是:",avg/len(scores))
8.有两个列表A和B,使用列表C来获取两个列表中公共的元素
A = [1,2,3,4,5,6,7,8]
B = [6,7,8,9,10,11,12]
C = []
for item in A:
if item in B:
C.append(item)
print(C)
9.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
scores = [6,8,8,9,7,6,5,4,]
max = scores[0]
for item in scores:
if item>max:
max = item
print(max)
10.获取列表中出现次数最多的元素
nums = [1, 2, 3,1,4,2,1,3,7,3,3]
count1 = 0
item1 = 0
for item in nums:
if nums.count(item) > count1:
count1 = nums.count(item)
item1 = item
print(item1)