2020-09-27列表的删除-切片-相关操作-相关方法

day5-列表的删除-切片-相关操作-相关方法

列表的删除

练习:删除指定分数列表中所有低于60分的成绩
坑一:直接遍历用remove删除元素-- 删不干净(因为遍历的时候没有把所有元素遍历出来)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
# scores = scores.copy() # scores1 = scores.copy()  # scores1 = scores[:]; scores1 = scores*1; scores1 = scores+[]

# 解决方法: 创建一个和原列表一样的列表,遍历新表列删除原列表
for i in scores.copy():
    if i < 60:
        scores.remove(i)
print(scores)

for i in scores[:]:
    if i < 60:
        scores.remove(i)
print(scores)

坑二:下标越界

scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
# 坑二:
for i in range(len(scores)):
    if scores[i] < 60:
        del scores[i]
print(scores)

# 解决方法:
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)
"""
len(scores) == 11
index == 0 0 < 11: if scores[0]==98 < 60 -> False  index = index +1  
index == 1 1 < 11: if scores[1]==45 < 60 -> True   del scores[1]   [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index == 1 1 < 11: if scores[1]==34 < 60 ->True   delscores[1]  [98, 89, 23, 67, 23, 9, 54, 100, 78]
index == 1 1 < 11: if scores[1]==89 < 60 ->False  index = index +1  
index == 2 2 < 11: if scores[2]==23 < 60 ->True   del scores[2]  [98, 89, 67, 23, 9, 54, 100, 78]
。。。

"""

列表的切片:

切片: - 获取列表中部分元素

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

    注意:

    • 1)列表切片的结果一定是列表

    • 2)结束下标对应的元素一定取不到

    • 3)a.如果步长为正,表示从前往后取,这个时候开始下标对应的元素必须在结束下标对应的元素的前面,否则是空的

      ​ b.如果步长为负,表示从后往前取,这个时候开始下标对应的元素必须在结束下标对应的元素的后面,否则为空

      list1 = [23,63,55,8,88,9,56,85]
      print(list1[1:4:1])
      print(list1[4:4:1])  # []
      print(list1[1:4:-1])  # []
      print(list1[4:1:-1])
      print(list1[1:-1:-1])   # []
      print(list1[-2:1:-2])  # [56, 8, 55]
      
  2. 切片省略

    1. 省略步长:列表[开始下标:结束下标] - 省略步长,步长就是1:

      fruits = ['苹果','香蕉','梨子','葡萄']
      print(fruits[1:3])  # ['香蕉','梨子']
      
    2. 省略开始下标: 列表[:结束下标:步长]

      """
      步长为正: 从第一个元素开始往后取
      步长为负:从最后一个元素开始往前取
      """
      
      fruits = ['苹果','香蕉','梨子','葡萄']
      print(fruits[:3])  # ['苹果','香蕉','梨子']
      print(fruits[:2:-1])  # ['葡萄']
      
    3. 省略结束下标:列表[开始下标::步长]

      """
      步长为正:从开始下标取到最后一个为止
      步长为负:从开始下标取到第一个元素为止
      """
      
      fruits = ['苹果','香蕉','梨子','葡萄']
      print(fruits[1::])  # ['香蕉','梨子','葡萄']
      print(fruits[1::-1])  # ['苹果','香蕉']
      
    4. 都省略:列表[::] - 倒序

      fruits = ['苹果','香蕉','石榴','葡萄']
      print(fruits[::])  # ['苹果', '香蕉', '石榴', '葡萄']
      print('hello'[::-1])  # olleh
      

列表的相关操作

  1. 加法运算和乘法运算

    1)加法:列表1 + 列表2 - 将列表1和列表2合并,产生一个新的列表

    list1 = [12,55,36]
    list2 = [1,2,3]
    print(list1+list2) # [12,55,36,1,2,3]
    '''
    注意:列表只能和列表相加
    print(list1 + 23) TypeError: can only concatenate list (not "int") to list
    '''
    
    1. 乘法:列表*N - 将列表中的元素重复N次,产生一个新的列表
    list1 = [12,5,66]
    list2 = list1*2
    print(list2,id(list1),id(list2))  # #  [12, 5, 66, 12, 5, 66]  45406488 43337168
    
  2. 比较运算: 两个列表之间支持比较大小和相等

    1. 两个列表比较大小 - 比较第一对不相等的元素的大小,谁大对应的列表就大
    list1 = [1,2,3,4,5]
    list2 = [10,20]
    print(list1 > list2)  # False
    print('adc' > 'dd')  # False
    print([] > [1,52])  # False
    
    print([1,2,5] > [1,'a',52])  #  TypeError: '>' not supported between instances of 'int' and 'str'
    
    1. 比较相等:元素顺序不同的两个列表不相等
    print([1,2,3] == [1,2,3])  # True
    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,30])  # True
    print(10 in [1,20,[10,25],30])  # False
    print([1,2] in [1,2,3])  #  False
    print([1,2] in [1,2,[1,2],3])  #  True
    
    #  练习:获取两个列表中功能元素
    # A = [1, 2, 5, 10, 3, 2]
    # B = [5, 2, 10, 20, 32, 2]
    # 求公共列表 C : [5, 2, 10]
    
    C = []
    for i in A:
        if i in B and i not in C:
            C.append(i)
    print(C)
    
    """
    i == 1  if 1 in B ->False  and  1 not in C -> True: -> False
    i == 2  if 2 in B ->True  and  2 not in C -> True: -> True   --> C = [2]
    i == 5  if 5 in B ->True  and  5 not in C -> True: -> True  --> C = [2,5]
    i == 10  if 10 in B ->True  and  10 not in C -> True: -> True  --> C = [2,5,10]  
    i == 3  if 3 in B ->False  and  10 not in C -> True: -> False 
    i == 2  if 2 in B ->True  and  2 not in C -> False: -> False (结束循环)
    """
    
  1. 相关函数

    sum、max、min、sorted、len、list

    1. sum(数字列表) - 求列表中所有元素的和
    scores = [52,88,96,48,75,25,63,100]
    print(sum(scores))
    
    1. max(列表)/min(列表) - 求列表元素的最大值和最小值(注意:列表中的类型必须一致,并且元素本身支持比较运算)
    scores = [52,88,96,48,75,25,63,100]
    print(max(scores),min(scores))
    
    1. sorted(列表) - 将列表中的元素从小到大排序(升序),产生一个新的列表(不会修改原列表)

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

    scores = [52,88,96,48,75,25,63,100]
    new_scores = sorted(scores)
    print(new_scores)
    
    scores = [52,88,96,48,75,25,63,100]
    new_scores = sorted(scores,reverse=True)
    print(new_scores)
    
    1. len(列表) - 获取列表长度(列表中元素的个数)

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

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

列表的相关方法

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

    names = ['犬夜叉','火影忍者']
    names.clear()
    print(names)  # []
    
    1. 列表.copy() - 复制指定列表产生一个一模一样的新列表(地址不同)这儿是浅拷贝
    names = ['犬夜叉','火影忍者']
    new_names = names.copy()
    print(new_names,id(names),id(new_names))  # ['犬夜叉', '火影忍者'] 19090008 19088848
    
    1. 列表.count(元素) - 统计指定元素在列表中出现的次数
    nums = [10,52,30,20,203,1,20,546,20,12]
    print(nums.count(20))  # 3
    print(nums.count(100))  #  0
    
    1. 列表.extend(序列) - 将序列中的元素全部添加到列表中
    names = ['犬夜叉','火影忍者']
    names.extend(['死亡笔记','一拳超人'])
    print(names)  # ['犬夜叉','火影忍者','死亡笔记','一拳超人']
    
    1. 列表.index(元素) - 获取指定元素在列表中的下标(返回的是0开始的下标值)
    names = ['犬夜叉','火影忍者','犬夜叉']
    # 如果元素有多个只取第一个下标的值
    print(names.index('犬夜叉'))  # 0
    
    # 如果不存在会报错
    # print(names.index('死神'))  # ValueError: '死神' is not in list
    
6. 列表.reverse()  - 列表倒序

7. 列表.sort()  -  将列表中的元素从下打到大排序

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

```python
nums = [10,52,30,20,203,1,20,546,20,12]
nums.sort()
nums.sort(reverse=True)
print(nums)
```

你可能感兴趣的:(2020-09-27列表的删除-切片-相关操作-相关方法)