总结
p列表 - list(增,删,改)
1. 增 - 添加元素
-
列表.append(元素) - 在列表的最后添加指定元素
names = ['李白', '高渐离', '韩信', '白起', '关羽', '艾莉', '安琪拉'] tvs = ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] tvs.append('生活大爆炸') print(tvs) # ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑', '生活大爆炸'] # 练习:用一个列表保存180以内所有能被3整除的数 list1 = [x for x in range(0, 100, 3)] print(list1) list2 = [] for x in range(0, 100, 3): list2.append(x) print(list2)
-
列表.insert(下标, 元素) - 在列表指定的下标位置前插入一个元素
tvs = ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑', '生活大爆炸'] tvs.insert(-1, '人民的名义') print(tvs) # ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑', '人民的名义', '生活大爆炸'] # 练习:已知有一个列表scores按分数从高到低保存了一个班的学生的分数,请插入一个新的分数,并保存列表分数从高到低排序 scores = [98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56] new_score = 78 for index in range(len(scores)): if new_score > scores[index]: scores.insert(index, new_score) break else: scores.append(new_score) print(scores) # [98, 93, 91, 87, 83, 80, 78, 77, 70, 65, 62, 56]
2. 删 - 删除列表中的元素
-
列表.remove(元素) - 直接删除顺序排序最前面的相等的第一个元素,如果该元素不存在则报ValueError错误提示
nums = [2, 3, 4, 6, 4, 3, 6] nums.remove(3) print(nums) # [2, 4, 6, 4, 3, 6] # nums.remove(10) # ValueError: list.remove(x): x not in list # 练习:删除tvs中最后一个3 nums = [2, 3, 4, 6, 4, 3, 6] for index in range(-1, -len(nums)-1, -1): if nums[index] == 3: del nums[index] break print(nums) # [2, 3, 4, 6, 4, 6] # 方法二: nums.reverse() # 倒序 nums.remove(3) nums.reverse() print(nums)
-
del 列表[下标] - 删除列表中指定下标对应的元素
tvs = ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] del tvs[1] print(tvs) # ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑']
-
列表.pop(下标) - 取出列表中下标对应的元素,当下标省略时,默认取出列表中最后一个元素(取出的意思就是有返回值)
tvs = ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] tv = tvs.pop(1) print(tv) # 非自然死亡 print(tvs) # ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] tv = tvs.pop() print(tv) # 亮剑 print(tvs) # ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传'] # tvs.pop(7) # IndexError: pop index out of range
3. 改 - 更改元素
-
列表[下标] = 值 - 修改列表中指定下标的元素
tvs = ['回家的诱惑', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] tvs[0] = '行尸走肉' print(tvs) # ['行尸走肉', '非自然死亡', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑'] # 练习:删除scores中所有小于60分的成绩 scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89, 30] while True: for index in range(len(scores)): if scores[index] < 60: del scores[index] break else: break print(scores) scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89, 30] lsit2 = [] for index in range(len(scores)): if scores[index] >= 60: lsit2.append(scores[index]) print(lsit2) scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89, 30] for index in range(len(scores)-1, -1, -1): if scores[index] < 60: del scores[index] print(scores) ''' scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89, 30] index -> range(10, -1, -1) -> 10~0 index = 10: if scores[10] < 60 -> if 30 < 60 -> del scores[10] -> scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89] index = 9: if scores[9] < 60 -> if 89 < 60 index = 8: if scores[8] < 60 -> if 76 < 60 index = 7: if scores[7] < 60 => if 23 < 60 => del scores[7] -> scores = [87, 34, 50, 23, 78, 99, 45, 76, 89] index = 6: if scores[6] < 60 => if 45 < 60 => del scores[6] => scores = [87, 34, 50, 23, 78, 99, 76, 89] index = 5: if scores[5] < 60 => if 99 < 60 index = 4: if scores[4] < 60 => if 78 < 60 index = 3: if scores[3] < 60 => if 23 < 60 => del scores[3] => scores = [87, 34, 50, 78, 99, 76, 89] index = 2: if scores[2] < 60 => if 50 < 60 => del scores[2] => scores = [87, 34, 78, 99, 76, 89] index = 1: if scores[1] < 60 => if 34 < 60 => del scores[1] => scores = [87, 78, 99, 76, 89] index = 0: if scores[0] < 60 => if 87 < 60 scores = [87, 78, 99, 76, 89] ''' scores = [87, 34, 50, 23, 78, 99, 45, 23, 76, 89, 30] temp = scores.copy() # temp = scores[:] for s in temp: if s < 60: scores.remove(s) print(scores)
4. 列表的加法和乘法运算
-
列表1 + 列表2 - 将两个列表合并产生一个新的列表
ls = [10, 20, 30] + ['abc', 123] print(ls) # [10, 20, 30, 'abc', 123]
-
列表 * N - 列表中的元素重复N次产生一个新的列表
list2 = [10, 20, 30] * 3 print(list2) # [10, 20, 30, 10, 20, 30, 10, 20, 30]
5. 有关列表操作的常用关键字(in 和 not in)
# 元素 in 列表 - 判断指定元素是否在指定列表中
# 元素 not in 列表 - 判断指定元素是否不在指定列表中
print(10 in [10, 20, 30]) # True
print([10] in [10, 20, 30]) # False
print([10, 2] in [10, 2, 49, [10, 2]]) # True
num = 0
if num == 10 or num == 20 or num == 30:
print('是')
if type(num) == int or type(num) == float:
print('是数字')
if type(num) in [int, float]:
print('是数字')
作业
1.已知一个数字列表,求列表中心元素。
nums = [90, 98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56, 23, 20, 1, 5]
nums_len = len(nums)
if nums_len % 2:
print('中心元素:', nums[nums_len // 2])
else:
print('中心元素:', nums[nums_len // 2 - 1], nums[nums_len // 2])
2.已知一个数字列表,求所有元素和。
nums = [90, 98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56, 23, 20, 1, 5]
# 方法一:
print('列表所有元素和是:', sum(nums))
# 方法二:
sum1 = 0
for num in nums:
sum1 += num
print('列表所有元素和是:', sum1)
3.已知一个数字列表,输出所有奇数下标元素。
nums = [90, 98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56, 23, 20, 1, 5]
for index in range(1, len(nums), 2):
print(nums[index], end=' ')
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
nums = [90, 98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56, 23, 20, 1, 5]
for num in nums:
if num % 2:
print(num, end=' ')
5.已知一个数字列表,将所有元素乘二。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums = [90, 98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56, 23, 20, 1, 5]
for index in range(len(nums)):
nums[index] *= 2
print(nums)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘大黄’, ‘张三’, ‘张三’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]
names = ['张三', '李四', '大黄', '大黄', '张三', '张三', '张三']
# 方法一:
print(list(set(names)))
# 方法二:
names_temp = []
for name in names:
if name not in names_temp:
names_temp.append(name)
print(names_temp)
7.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
scores = [9.0, 8.7, 8.3, 9.2, 8.6, 8.7, 9.4, 9.4]
min_score = min(scores)
max_score = max(scores)
# 如果不用min和max函数
min_score = scores[0]
max_score = scores[0]
for score in scores:
if max_score < score:
max_score = score
if min_score > score:
min_score = score
scores.remove(min_score)
scores.remove(max_score)
print('平均分:', round((sum(scores) / len(scores)), 2)) # round(数字, x) - 保留小数点后x(int类型)位小数
8.有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, ‘a’, 4, 90] B = [‘a’, 8, ‘j’, 1] --> C = [1, ‘a’]
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
C = []
for _A in A:
if _A in B and _A not in C:
C.append(_A)
print(C)
9.*有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
例如: nums = [19, 89, 90, 600, 1] —> 600
nums = [19, 89, 90, 600, 1]
max_num = nums[0]
for num in nums:
if max_num < num:
max_num = num
print('最大值是:', max_num)
10.*获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1, 2, 3, 1, 4, 2, 1, 3, 7, 3, 3]
# 方法一:
# 思想:求出每一个不同的元素出现的次数,互相比较找出次数最大的那个,并输出对应元素
max_count = 0
temp = []
counts = []
for num in nums:
if num in temp:
index = temp.index(num)
counts[index] += 1
else:
temp.append(num)
counts.append(1)
max_count = max(counts)
print('出现最多的元素是:', end=' ')
for index in range(len(counts)):
if counts[index] == max_count:
print(temp[index], end=' ')
# 方法二:
# 思想:每次循环都从列表中取出一个互不相同的元素,直到列表中没有元素,则最后一次取出的元素即位列表中出现次数最多的元素
temp = []
while True:
temp.clear()
for index in range(len(nums)-1, -1, -1):
if nums[index] not in temp:
temp.append(nums.pop(index))
if len(nums) == 0:
break
print('出现最多的元素是:', end=' ')
for num in temp:
print(num, end=' ')