python基础——数据类型(四)列表

如有兴趣了解更多请关注我的个人博客https://07xiaohei.com/

(一)概述:

list列表的本质是一种有序的集合,是可变的序列(具有先后顺序),是python最基本的数据结构,。

列表元素的个数没有限制,元素的类型是任意的(只要python支持),不同类型的元素也可以存储在同一列表中。

列表的长度是系统内置的,可以调用len()函数获得列表长度。

本章将会讲述所有列表相关操作,包括创建,索引访问,切片访问,增删改查、以及各类操作。

(二)创建与形式:

python中用[]方括号表示列表,并用逗号分割其中的元素。

形式为: 列表名 = [元素1,元素2…] / 列表名=[](空列表)

list1 = []     #空列表
list2 = list() #格式化,空列表
list3 = [10, 20, 30, 3.14, 6.28, "aaa", "bbb", [1,2,3,4], True, False, 3+7j, {"a":1}, "\n"]
print(list3)
#运算结果:
# [10, 20, 30, 3.14, 6.28, 'aaa', 'bbb', [1, 2, 3, 4], True, False, (3+7j), {'a': 1}, '\n']

(三)列表的特性:

1. 访问:

  1. 索引访问:

    格式:listname[index],listname是列表名字,index是索引值。

    index的取值范围为**[0,列表长度-1]**

    索引值有正负之分。

    正索引值从0开始对应第一个元素,并以此类推。

    负索引值从-1开始对应最后一个元素,是反向访问。

    listprint = [1,2,3,4,5,6,7,8,9,10,"11","12","13"]
    for i in range (0,13):
        print(listprint[i],end="  ")
    print("")
    for i in range(0, 13):
        print(listprint[-i-1],end="  ")
    print('')
    # 运算结果:
    # 1  2  3  4  5  6  7  8  9  10  11  12  13
    # 13  12  11  10  9  8  7  6  5  4  3  2  1
    
  2. 切片访问:

    格式:listname[start: end: step],listname是列表名字,start是开始索引,默认为1,end是结束索引,默认为列表末尾,step是步长,默认为1。

    开始索引到结束索引是一个左闭右开区间,表示截取此部分列表内容进行访问。步长是从开始索引开始每次访问相邻元素的距离。

    三者均可以是负值,也可以不同时为正或负,两个索引为负代表反向索引,步长为负代表从结束索引向开始索引移动。

    listprint = [1,2,3,4,5,6,7,8,9,10,"11","12","13"]
    print(listprint[::])     #相当于正向遍历
    
    print(listprint[::-1])   #相当于反向遍历
    
    print(listprint[2:6])    #相对于访问第3-6个元素
    
    print(listprint[6:2])    #但结束索引小于开始索引,直接结束、
    
    print(listprint[-2:-6])  #同上
    
    print(listprint[-6:-2])  #相当于访问倒数第6-3个元素
    
    print(listprint[1:8:2])  #相当于访问1,3,5,7索引元素
    
    print(listprint[8:1:2])  #直接结束
    
    print(listprint[1:8:-2]) #要反向访问,开始索引应大于结束索引,否则直接结束
    
    print(listprint[8:1:-2]) #相当于访问8,6,4,1索引元素
    
    print(listprint[-1:-8:2])#直接结束
    
    print(listprint[-8:-1:2])#相当于访问-8,-6,-4,-2元素
    
    print(listprint[-1:-8:-2])#相当于访问-1,-3,-5,-7元素
    
    print(listprint[-8:-1:-2])#直接结束
    
    # 运算结果:
    #1  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '11', '12', '13']
    #2  ['13', '12', '11', 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    #3  [3, 4, 5, 6]
    #4  []
    #5  []
    #6  [8, 9, 10, '11']
    #7  [2, 4, 6, 8]
    #8  []
    #9  []
    #10 [9, 7, 5, 3]
    #11 []
    #12 [6, 8, 10, '12']
    #13 ['13', '11', 9, 7]
    #14 []
    
2. 删除列表:

python自带垃圾回收机制,已创建的列表如不再使用,将会被编译器自动回收,不会产生内存垃圾。

如果需要可以手动删除,使用del 关键字手动删除列表。

形式为: del listname

a=[1,2,3,4,5]
del a
print(a) #此句无法输出,因为a已经被删除了,a变量处于未定义状态
3.多维列表:

多维列表是将其他列表当做列表的元素放在一个列表当中,也就是列表的嵌套,一般我们常用的是二位列表,这里以二维列表为例。

二维列表形式为:

listname = [ sublistname1, sublistname2 , sublistname3, … , sublistnamen ]

访问形式为:

listname[ index1] [index2]

index1代表索引中的行,代表二维列表中第index1-1个列表;

index2代表索引中的列,代表二维列表中第index1-1个列表的第index2-1个元素。

如果是多维列表,逻辑同一维列表转变为二维列表。

a=[[1,2,3],[4,5,6],[7,8,9]]
print(a[0],a[1],a[2],sep=" ",end="\n")
print(a[0][0],a[1][0],a[2][0],sep=" ",end="\n")
print(a[0][1],a[1][1],a[2][1],sep=" ",end="\n")
print(a[0][2],a[1][2],a[2][2],sep=" ",end="\n")
# 输出结果
# [1, 2, 3] [4, 5, 6] [7, 8, 9]
# 1 4 7
# 2 5 8
# 3 6 9
4. 列表的主要操作(增删改查):
  • 修改列表元素:

    • 单个元素的修改:

      形式:list1[index] = value,就可以修改列表中对应索引的内容为value。

      a = [1,2,3]
      a[1]=4
      print(a)
      # 输出结果:
      # [1, 4, 3]
      
    • 一组元素的修改:

      实际上是使用切片的形式给一组元素赋值。此方法不能给单个数字赋值,也会把字符串自动转换为列表,每个字符对应一个元素。

      如果不指定步长,新赋值的元素个数可以和要修改的列表元素个数不相同(实际上,这种方法可以为列表删除/添加元素)。

      如果指定步长,要求新赋值的元素个数和要修改的列表元素个数相同。

      listdisplay = [1,2,3,4,5,6,7,8]
      print("修改前:",listdisplay)
      listdisplay[0: 2] = ["a","b"]
      print("修改后:",listdisplay)
      
      # listdisplay = [1,2,3,4,5,6,7,8]
      # print("修改前:",listdisplay)
      # listdisplay[0: 0] = 9  #此句话错误,不能用切片方式对数字单独赋值。
      
      listdisplay = ["1","2","3","4"]
      print("修改前:",listdisplay)
      listdisplay[0: 0] = "5678"  #此句话错误,不能用切片方式对数字单独赋值。
      print("修改后:",listdisplay)  #可以用切片方式对数字单独赋值,而且会改变元素个数
      
      listdisplay = []
      print("修改前:",listdisplay)
      listdisplay[0: 0] = ["1","2","3","4"]  # 对空切片赋值
      print("修改后:",listdisplay)
      
      listdisplay = ["1","2","3","4"]
      print("修改前:",listdisplay)
      listdisplay[:] = ["5","6","7","8","9"]  # 新赋值的元素个数与原来的元素个数可以不相同。并且元素个数增加了。
      print('修改后:', listdisplay)
      
      listdisplay = ["1","2","3","4"]
      print("修改前:",listdisplay)
      listdisplay[:] = ["5","6","7","8","9"]  # 新赋值的元素个数与原来的元素个数可以不相同。并且元素个数减少了。
      print('修改后:', listdisplay)
      
      listdisplay = ["1","2","3","4","5","6"]
      print("修改前:",listdisplay)
      listdisplay[1: 6: 2] = ['a', 'b', 'c']  # 从索引 1 开始每隔两个添加一个新元素,到索引 6 ,不包括 索引 6
      print('修改后:', listdisplay)
      
      # 运行结果:
      # 修改前: [1, 2, 3, 4, 5, 6, 7, 8]
      # 修改后: ['a', 'b', 3, 4, 5, 6, 7, 8]
      # 修改前: ['1', '2', '3', '4']
      # 修改后: ['5', '6', '7', '8', '1', '2', '3', '4']
      # 修改前: []
      # 修改后: ['1', '2', '3', '4']
      # 修改前: ['1', '2', '3', '4']
      # 修改后: ['5', '6', '7', '8', '9']
      # 修改前: ['1', '2', '3', '4']
      # 修改后: ['5', '6', '7', '8', '9']
      # 修改前: ['1', '2', '3', '4', '5', '6']
      # 修改后: ['1', 'a', '3', 'b', '5', 'c']
      
  • 查询列表元素:

    • 使用 in 和 not in判断元素是否在列表内:

      格式: value in listname / value not in listname

      可以判断value值是否在listname中,整体为布尔值。

      listdisplay = [1,2,3,4,5,6,7]
      print(1 in listdisplay)
      print(3 not in listdisplay)
      print(8 in listdisplay)
      print(10 not in listdisplay)
      # 输出结果:
      # True
      # False
      # False
      # True
      

      此方法配合下面的index方法食用更佳(先判断在不在列表内部,再用index方法,以免index没查到而报错)。

    • 使用列表的index()方法查找元素:

      格式:listname.index(obj, start, end)

      返回值为查找到的元素第一次在列表中出现的索引值。

      obj表示要查找的元素;

      start表示起始索引,默认为列表初始元素;

      end表示结束索引,默认为列表末尾元素;(检查不包括end元素本身)

      start和end均可不写,此时检索全列表。

      listdisplay = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      getindex1 = listdisplay.index(3)  # 整个列表中检索元素3的位置
      print(getindex1)
      
      getindex2 = listdisplay.index(10,0,len(listdisplay)+1) #和上面查询范围等价
      print(getindex2)
      
      getindex3 = listdisplay.index(5,0,5)  # 检索元素5在列表索引0-4中的位置
      print(getindex3)
      
      # getindex4 = listdisplay.index(6, 0, 5)  #检查元素6在列表索引中0-4的位置,但是不在其中,报错
      # 输出结果:
      # 2
      # 9
      # 4
      
    • 使用列表的count()方法查找元素:

      格式:listname.count(obj)

      返回值为查找元素在列表中出现的次数,返回0意味着列表中未出现此元素。

      obj为要统计的元素。

      不能指定查询列表的某个范围,只能全局查找。

      listdisplay = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 3, 3]
      getcount1 = listdisplay.count(3)  # 整个列表中检索元素3的数量
      print(getcount1)
      getcount2 = listdisplay.count(12)  # 整个列表中检索元素12的数量,返回0,即未出现
      print(getcount2)
      # 输出结果:
      # 4
      # 0
      
      
  • 添加列表元素:

    • 列表的组合(使用"+"符号):

      格式:listname = listname1+listname2+…+listnamen

      "+"运算符在操作多个列表时,可以将列表拼接到一起,生成一个新的列表,新列表按所加的顺序生成所有元素。

      此方法虽然简便直观,但是执行效率低,不建议经常使用。

      listdisplay1 = [1, 2, 3, 4, 5]
      listdisplay2 = [6, 7, 8, 9, 10]
      listdisplay3 = listdisplay1+listdisplay2
      print(listdisplay1)
      print(listdisplay2)
      print(listdisplay3)
      # 运行结果:
      # [1, 2, 3, 4, 5]
      # [6, 7, 8, 9, 10]
      # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      
    • 列表的重复(使用"*"符号):

      格式:listname= listname1 ***** n

      "*"运算符对单个列表操作时,将被操作列表复制n遍合成一个列表,相当于用当前元素生成长度为原长度n倍的列表。

      同样,此方法执行效率不高。

      listdisplay = [1, 2, 3, 4, 5]
      listdisplaynew = listdisplay*3
      print(listdisplay)
      print(listdisplaynew)
      # 输出结果:
      # [1, 2, 3, 4, 5]
      # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
      
    • append()方法添加元素:

      格式:listname.append(obj)

      append()函数添加元素至列表末尾,obj可以是一个列表/元组等等,但是obj会作为一个整体被添加进入listname这个列表中,也就是只添加一个元素,形成一个新列表(但地址不变,只是长度和元素更新了)。

      listdisplay=[1,2,3,4]
      listdisplay.append(5)
      print(listdisplay)
      listdisplay.append("12345")
      print(listdisplay)
      listdisplay.append([6,7,8,9,10])
      print(listdisplay)
      listdisplay.append({11,12,13,14,15})
      print(listdisplay)
      # 输出结果:
      # [1, 2, 3, 4, 5]
      # [1, 2, 3, 4, 5, '12345']
      # [1, 2, 3, 4, 5, '12345', [6, 7, 8, 9, 10]]
      # [1, 2, 3, 4, 5, '12345', [6, 7, 8, 9, 10], {11, 12, 13, 14, 15}]
      
    • extend()方法添加元素:

      格式:listname.extend(obj)

      extend()函数添加元素至列表末尾,obj可以是一个字符串/列表/元组,但是不能是单个数据,必须是这种序列形式,哪怕里面只有一个元素。

      extend()函数会将obj包含的所有元素逐个提取加入listname列表中,也就是被添加的数据不止一个,形成一个新列表(地址不变,只是长度和元素更新了)。

      listdisplay=[1,2,3,4]
      listdisplay.extend([5])
      print(listdisplay)
      listdisplay.extend("12345")
      print(listdisplay)
      listdisplay.extend([6,7,8,9,10])
      print(listdisplay)
      listdisplay.extend({11,12,13,14,15})
      print(listdisplay)
      # # 输出结果:
      # [1, 2, 3, 4, 5]
      # [1, 2, 3, 4, 5, '1', '2', '3', '4', '5']
      # [1, 2, 3, 4, 5, '1', '2', '3', '4', '5', 6, 7, 8, 9, 10]
      # [1, 2, 3, 4, 5, '1', '2', '3', '4', '5', 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      
    • insert()方法添加元素:

      格式:listname.insert(index , obj)

      insert()函数可以在列表的指定位置插入元素,通过index索引值确定插入位置,在此位置的原来的元素默认后移为新插入元素空出空间。

      obj的插入和append的情况相同。

      listdisplay=[1,2,3,4]
      listdisplay.insert(1,[1.5])
      print(listdisplay)
      listdisplay.insert(0,"12345")
      print(listdisplay)
      listdisplay.insert(4,[6,7,8,9,10])
      print(listdisplay)
      listdisplay.insert(3,{11,12,13,14,15})
      print(listdisplay)
      # # 输出结果:
      # [1, [1.5], 2, 3, 4]
      # ['12345', 1, [1.5], 2, 3, 4]
      # ['12345', 1, [1.5], 2, [6, 7, 8, 9, 10], 3, 4]
      # ['12345', 1, [1.5], {11, 12, 13, 14, 15}, 2, [6, 7, 8, 9, 10], 3, 4]
      
  • 删除列表元素:

    • del关键字进行删除:

      格式:del listname[index]

      del listname[start : end]

      del关键字可以根据列表的索引值进行指定值/范围删除,也可以直接删除整个列表(见上),可以指定超过范围的索引值,但不建议。

      删除后列表的长度和元素均进行更新。

      end本身不会被删除。

      listdisplay=[1,2,3,4,5,6,7,8,9,10]
      del listdisplay[1]   #删一个
      print(listdisplay)
      del listdisplay[1:5] #删四个
      print(listdisplay)
      del listdisplay[6:7] #无效操作
      print(listdisplay)
      del listdisplay[-2:-1] #删除倒数第二个
      print(listdisplay)
      # # 输出结果:
      # [1, 3, 4, 5, 6, 7, 8, 9, 10]
      # [1, 7, 8, 9, 10]
      # [1, 7, 8, 9, 10]
      # [1, 7, 8, 10]
      
    • pop方法进行删除:

      格式:listname.pop(index)

      pop()函数同样根据列表的索引值进行指定值删除,不能指定范围,不能整个列表删除。

      index的默认值为列表最后一个元素,其值不能超过范围。

      listdisplay=[1,2,3,4,5,6,7,8,9,10]
      listdisplay.pop(1)   #删位置为1的元素
      print(listdisplay)
      listdisplay.pop()  #删除最后一个元素
      print(listdisplay)
      listdisplay.pop(-2) #删除倒数第二个
      print(listdisplay)
      # 输出结果:
      # [1, 3, 4, 5, 6, 7, 8, 9, 10]
      # [1, 3, 4, 5, 6, 7, 8, 9]
      # [1, 3, 4, 5, 6, 7, 9]
      
    • remove方法进行删除:

      格式:listname.remove(element)

      remove()函数根据元素本身的值(element)进行删除,且只删除第一个指定元素值,随后结束函数,不会删除其他相同元素。

      被删除元素必须在列表中存在,否则会报错。

      listdisplay=[1,2,3,4,5,6,7,2,8,9,10]
      listdisplay.remove(2) #删除第一个值为2的元素,第二个不删除
      print(listdisplay)
      listdisplay.remove(2) #删除值为2的元素
      print(listdisplay)
      # listdisplay.remove(-2)   #-2不存在,报错
      # 输出结果:
      # [1, 3, 4, 5, 6, 7, 2, 8, 9, 10]
      # [1, 3, 4, 5, 6, 7, 8, 9, 10]
      
    • clear方法进行删除:

      格式:listname.clear()

      clear()函数删除所有元素,但变量名还存在(区别于上面del 删除列表操作)。

      listdisplay=[1,2,3,4,5,6,7,2,8,9,10]
      print(listdisplay)
      listdisplay.clear()
      print(listdisplay)
      # 输出结果:
      # [1, 2, 3, 4, 5, 6, 7, 2, 8, 9, 10]
      # []
      
5. 列表的其他操作:
  • 获得最大,最小值:

    格式:max(listname) / min(listname)

    获得列表最大值/最小值。(只限于可比较类型比较而出的最大/最小值)。

    listdisplay = ["ayz","bcd","jkl","w"] #按首字母的ASCII码比较
    print(max(listdisplay))
    # 输出结果:
    # w  
    
  • 排序:

    • 列表的sort方法排序:

      格式:listname.sort( ***** , key=None, reverse=False)

      key:指定带有一个参数的函数,用于从每个序列元素中提取比较键。对应于序列中的每一项的键会被计算一次,然后在整个排序过程中使用,默认值None代表直接对序列项排序,不计算单独的键值。

      reverse:为一个布尔值,默认False,表示升序排序,如果指定了True,则表示降序排序

      sort方法对原列表进行修改。

      listdisplay = [8,6,4,6,8,1,9]
      listdisplay.sort(reverse=True)
      print(listdisplay)
      listdisplay.sort()
      print(listdisplay)
      listdisplay2= ["abc","qwe","jkl","zxc"]
      listdisplay2.sort(reverse=False)
      print(listdisplay2)
      # 输出结果:
      # [9, 8, 8, 6, 6, 4, 1]
      # [1, 4, 6, 6, 8, 8, 9]
      # ['abc', 'jkl', 'qwe', 'zxc']
      
    • 内置的sorted()函数排序:

      格式:sorted(iterable, ***** ,key=None, reverse=False)

      sorted()函数从一个可迭代对象根据iterable的项返回一个新的排序列表

      key和reverse同上。

      listdisplay = [8,6,4,6,8,1,9]
      listdisplay2=sorted(listdisplay,reverse=True)
      print(listdisplay)
      print(listdisplay2)
      listdisplay3=sorted(listdisplay)
      print(listdisplay)
      print(listdisplay3)
      listdisplay2= ["abc","qwe","jkl","zxc"]
      listdisplay4=sorted(listdisplay2,reverse=False)
      print(listdisplay4)
      # 输出结果:
      # [8, 6, 4, 6, 8, 1, 9]
      # [9, 8, 8, 6, 6, 4, 1]
      # [8, 6, 4, 6, 8, 1, 9]
      # [1, 4, 6, 6, 8, 8, 9]
      # ['abc', 'jkl', 'qwe', 'zxc']
      
  • 复制:

    • 直接赋值:

      = 直接赋值是非拷贝方法。

      两个列表是等价的,修改其中任何一个列表都会影响另一个列表。

      listdisplay = [8,6,4,6,8,1,9]
      listdisplay1= listdisplay
      listdisplay1[1]=0
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [8, 0, 4, 6, 8, 1, 9]
      # [8, 0, 4, 6, 8, 1, 9]
      
    • copy方法(浅拷贝):

      格式:listnewname = listname.copy()

      python中浅拷贝是指list保存的地址同样被复制过去,而不是创建新的地址复制值过去。

      copy方法对嵌套的list只能对第一层实现深拷贝,而其内嵌套的list仍是浅拷贝。

      listdisplay = [1,2,3,[1,2,3]]
      listdisplay1=listdisplay.copy()
      listdisplay[0]=3
      listdisplay[3][1]=3
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [3, 2, 3, [1, 3, 3]]
      # [1, 2, 3, [1, 3, 3]]
      
    • 使用切片:

      格式:listnewname= listname[:]

      同为浅拷贝。

      listdisplay = [1,2,3,[1,2,3]]
      listdisplay1=listdisplay[:]
      listdisplay[0]=3
      listdisplay[3][1]=3
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [3, 2, 3, [1, 3, 3]]
      # [1, 2, 3, [1, 3, 3]]
      
    • for循环遍历:

      无具体格式,是一个操作流程。

      同为浅拷贝。

      listdisplay = [1,2,3,[1,2,3]]
      listdisplay1=[]
      for i in range(len(listdisplay)):
          listdisplay1.append(listdisplay[i])
      listdisplay[0]=3
      listdisplay[3][1]=3
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [3, 2, 3, [1, 3, 3]]
      # [1, 2, 3, [1, 3, 3]]
      
    • 列表生成式:

      格式:listnewname = [i for i in listname ]

      同为浅拷贝。

      listdisplay = [1,2,3,[1,2,3]]
      listdisplay1=[i for i in listdisplay]
      listdisplay[0]=3
      listdisplay[3][1]=3
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [3, 2, 3, [1, 3, 3]]
      # [1, 2, 3, [1, 3, 3]]
      
    • deepcopy方法:

      格式:listnewname= copy.deepcopy(listname)

      是深拷贝,原来的列表和新列表完全无关。

      需要import copy库

      import copy
      listdisplay = [1,2,3,[1,2,3]]
      listdisplay1 = copy.deepcopy(listdisplay)
      listdisplay[0]=3
      listdisplay[3][1]=3
      print(listdisplay)
      print(listdisplay1)
      # 输出结果:
      # [3, 2, 3, [1, 3, 3]]
      # [1, 2, 3, [1, 2, 3]] #唯一未随之改变的
      
  • 翻转:

    格式: listname.reverse()

    将列表元素倒叙,对原列表操作,不返回新列表。

    不会翻转里面的内嵌套列表/字符串/元组,只是更换元素位置。

    listdisplay = [1,2,3,[1,2,3]]
    listdisplay.reverse()
    print((listdisplay))
    # 输出结果:
    # [[1, 2, 3], 3, 2, 1]
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaohei07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值