小白学习Python的第五天

一. Python中的重复删除

# 练习:删除指定分数列表中所有低于60分的成绩
# scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
# 删除后:[98, 89, 67, 100, 78]
# 坑一:直接遍历用remove删除元素 - 删除不干净(因为遍历的时候没有把所有元素都遍历出来)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
new = scores[:]
for i in new:
    if i < 60:
        scores.remove(i)
print(scores)
# 解决坑一:创建一个和原列表一样的新列表,遍历新列表,删除原列表
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
for i in scores[:]:   # 新列表的产生方式:scores[:]、scores.copy()、scores+[]、scores*1
    if i < 60:
        scores.remove(i)
print(scores)
# 坑二:报错(下标越界)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
 len(列表) - 获取列表中元素的个数
for index in range(len(scores)):
    if scores[index] < 60:
        del scores[index]
print(scores)
"""
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index in range(11) -> 0~10
index = 0: if 98<60
index = 1: if 45<60 -> del scores[1] -> scores = [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 2: if 89<60
index = 3: if 23<60 -> del scores[3] -> scores = [98, 34, 89, 67, 23, 9, 54, 100, 78]
index = 4: if 23<60 -> del scores[4] -> scores = [98, 34, 89, 67, 9, 54, 100, 78]
index = 5: if 54<60 -> del scores[5] -> scores = [98, 34, 89, 67, 9, 100, 78]
index = 6: if 78<60
index = 7: if scores[7] <60 (报错,下标越界!)
"""
# 解决坑二:
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0
while index < len(scores):
    s = scores[index]
    if s < 60:
        del scores[index]
    else:
        index += 1
print(scores)
"""
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0: if 98<60
index = 1: if 45<60 -> del scores[1] -> scores = [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 1: if 34<60 -> del scores[1] -> scores = [98, 89, 23, 67, 23, 9, 54, 100, 78]
index = 1: if 89<60
index = 2: if 23<60 -> del scores[2] -> scores = [98, 89, 67, 23, 9, 54, 100, 78]
index = 2: if 67<60
index = 3: if 23<60 -> del scores[2] -> scores = [98, 89, 67, 9, 54, 100, 78]  
index = 3: if 9<60 -> [98, 89, 67, 54, 100, 78]
index = 3: if 54<60 -> [98, 89, 67, 100, 78] 
index = 3: if 100<60
index = 4: if 78<60
(循环结束)   
"""

# 方法三:
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
for index in range(len(scores)-1, -1, -1):
    if scores[index] < 60:
        del scores[index]
print(scores)

二. 列表的切片

列表切片:获取列表中部分元素(一次性获取多个元素)

"""
语法:
列表[开始下标:结束下标:步长] - 从开始下标开始获取到结束下标前为止,每次增加步长

注意:
1)列表切片的结果一定是列表
2)结束下标对应元素一定取不到
3)
a.如果步长为正,表示从前往后取(从开始下标到结束下标),这个时候开始下标对应的元素必须在结束下标对应的元素的前面,否则结果为空
b.如果步长为负,表示从后往前取(从开始下标到结束下标),这个时候开始下标对应的元素必须在结束下标对应的元素的后面,否则结果为空
"""

list1 = [23, 45, 67, 89, 45, 67, 32]
print(list1[1:4:1])   # [45, 67, 89]
print(list1[0:6:2])   # [23, 67, 45]
print(list1[2:5:1])   # [67, 89, 45]
print(list1[3:3:1])   # []
print(list1[1:4:-1])  # []
print(list1[1:-1:-1])  # []
print(list1[-2:1:-2])  # [67, 89]
# 2.切片省略
"""
1)省略步长
列表[开始下标:结束下标] - 省略步长,步长就是1:列表[开始下标:结束下标:1]
"""
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[1:4])   # ['香蕉', '猕猴桃', '橘子']
print(fruits[-3:-1])   # ['橘子', '石榴']
print(fruits[-1:2])   # []

"""
2)省略开始下标
列表[:结束下标:步长]

步长为正:从第一个元素开始往后取(相当于开始下标是0)
步长为负:从最后一个元素开始往前取(相当于开始下标是-1)
"""
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[:4])   # ['苹果', '香蕉', '猕猴桃', '橘子']
print(fruits[:3:-2])   # ['葡萄']
print(fruits[:3:2])   # ['苹果', '猕猴桃']

"""
3)省略结束下标
列表[开始下标::步长]

步长为正:取到最后一个元素为止
步长为负:取到第一个元素为止
"""
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[2:])   # ['猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[-3::-1])   # ['橘子', '猕猴桃', '香蕉', '苹果']
print(fruits[3::-2])   # ['橘子', '香蕉']

print(fruits[:])   # ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[::-1])   # ['葡萄', '石榴', '橘子', '猕猴桃', '香蕉', '苹果']

三. 列表相关的操作

1.加法运算和乘法运算
1)加法:列表1 + 列表2 - 合并两个列表产生一个新的列表

list1 = [10, 20, 30]
list2 = [100, 200, 300]
list3 = list1 + list2
print(list3)   # [10, 20, 30, 100, 200, 300]

# 注意:列表只能和列表相加
# print(list1 + 100)  # TypeError: can only concatenate list (not "int") to list

2)乘法:列表N / N列表 - 将列表中的元素重复N次产生一个新的列表

list4 = list1 * 3
print(list4)   # [10, 20, 30, 10, 20, 30, 10, 20, 30]

list5 = list1*1
print(list5)   # [10, 20, 30]
list6 = list1
print(list6)   # [10, 20, 30]
print(id(list1), id(list5), id(list6))   # 2754335016136 2754335062280 2754335016136

2.比较运算
两个列表之间支持比较大小和相等
1)两个列表比较大小 - 比较第一对不相等的元素的大小,谁大对应的列表就大

list11 = [1, 2, 3, 4, 5]
list22 = [10, 20]
print(list11 > list22)   # False

# print([1, '1'] > [1, 2])   # TypeError: '>' not supported between instances of 'str' and 'int'

2)比较相等

# 元素顺序不同的两个列表不相等
print([1, 2, 3] == [1, 3, 2])   # False
print([1, 2, 3] == 'abc')   # False

3.in 和 not in
元素 in 列表 - 判断列表中是否存在指定的元素
元素 not in 列表 - 判断列表中是否不存在指定的元素

print(10 in [1, 20, 10, 4])   # True
print(10 not in [1, 20, 10, 4])   # False
print(10 in [1, 2, 3, [10, 20]])   # False
print([1, 2] in [1, 2, 3, 4])   # False
# 练习:获取两个列表中公共元素
# A = [1, 2, 5, 10]
# B = [5, 2, 10, 20, 32]
# 求公共列表C:[5, 2, 10]

# 方法一:
A = [1, 2, 5, 10]
B = [5, 2, 10, 20, 32]
C = []
for i in A:
    if i in B and i not in C:
        C.append(i)
print(C)

# 方法二:
for i in A:
    if i in B:
        C.append(i)
print(list(set(C)))

4.相关函数
sum、max、min、sorted、len、list
1)sum(数字列表) - 求列表中所有元素的和

scores = [34, 90, 89, 64, 70]
print(sum(scores))   # 347

2)max(列表)/min(列表) - 求列表中元素的最大/最小值(注意:列表中元素的类型必须一致,并且元素本身支持比较运算)

print(max(scores))   # 90
print(min(scores))   # 34

3)
sorted(列表) - 将列表中的元素从小到大排序(升序),产生一个新的列表(不会修改原列表)
sorted(列表, reserve=True) - 将列表中的元素从大到小排序(降序),产生一个新的列表(不会修改原列表)

scores = [34, 90, 89, 64, 70]
print(sorted(scores))

scores = [34, 90, 89, 64, 70]
print(sorted(scores, reverse=True))

4)len(列表) - 获取列表长度(列表中元素的个数)

print(len(scores))   # 5

5)list(数据) - 将指定数据转换成列表(数据必须是序列;转换的时候直接将序列中的元素作为新的列表的元素)

print(list('abc'))   # ['a', 'b', 'c']
print(list(range(4)))   # [0, 1, 2, 3]

四. 列表相关的方法

1.列表.clear() - 清空指定列表

names = ['犬夜叉', '火影忍者', '海贼王']
names.clear()
print(names)

2.列表.copy() - 复制指定列表产生一个一模一样的新列表(地址不同)

# 这儿是浅拷贝
names = ['犬夜叉', '火影忍者', '海贼王']
new_names = names.copy()
print(new_names, id(names), id(new_names))   # ['犬夜叉', '火影忍者', '海贼王'] 2440261207304 2440261207240

3.列表.count(元素) - 统计指定元素在列表中出现的次数

nums = [23, 89, 10, 89, 10, 6, 85]
print(nums.count(10))   # 2
print(nums.count(23))   # 1
print(nums.count(100))  # 0

4.列表.extend(序列) - 将序列中所有的元素全部添加到列表中

names = ['犬夜叉', '火影忍者', '海贼王']
names.extend(['死神', '死亡笔记'])
print(names)   # ['犬夜叉', '火影忍者', '海贼王', '死神', '死亡笔记']

names = ['犬夜叉', '火影忍者', '海贼王']
names.extend('abc')
print(names)   # ['犬夜叉', '火影忍者', '海贼王', 'a', 'b', 'c']

5.列表.index(元素) - 获取指定元素在列表中的下标(返回的是0开始的下标值)

names = ['犬夜叉', '火影忍者', '海贼王']
print(names.index('火影忍者'))   # 1

# 如果元素不存在会报错
# print(names.index('一拳超人'))   # ValueError: '一拳超人' is not in list

# 如果元素有多个,只取第一个的下标
names = ['犬夜叉', '火影忍者', '海贼王', '犬夜叉']
print(names.index('犬夜叉'))   # 0

6.列表.reverse() - 列表倒序(不会产生新列表,直接修改原列表的顺序)

names = ['犬夜叉', '火影忍者', '海贼王']
names.reverse()
print(names)   # ['海贼王', '火影忍者', '犬夜叉']

7.列表.sort() - 将列表中的元素从小到大排序(不会产生新的列表)
列表.sort(reverse=True) - 将列表中的元素从大到小排序

nums = [23, 89, 10, 89, 10, 6, 85]
nums.sort()
print(nums)   # [6, 10, 10, 23, 85, 89, 89]
print(nums.sort())   # None(表示没有)

猜你喜欢

转载自blog.csdn.net/bbbbbya/article/details/108832415