zoukankan      html  css  js  c++  java
  • 内置数据结构(list)

    列表、元组、字符串、字典和集合是python内置的数据结构,也可以叫内置容器。前3个是线性结构,线性结构可以切片操作、解包和封包操作。

    dir()方法可以查看对象拥有哪些属性和方法。

    help()方法可以查看一个方法的原形。

    len()方法可以返回列表、元组中元素的个数。

    id()方法查看一个变量的id值。

    一、列表(list)

    lt = list()  #定义一个空列表

    lt = [] #同上

    lt = [1, 2, 3, 4, 5]

    1.索引操作

    列表的索引是从0开始。负数索引表示从后往前,由-1开始,-1表示最后一个元素。如果索引超出范围,将抛出一个IndexError异常。

    lt[0]  #结果为:1

    lt[-2] #结果为:4

    lt[9]
    
    Traceback (most recent call last):
      File "<pyshell#4>", line 1, in <module>
        lt[9]
    IndexError: list index out of range
    

      #修改指定索引的值,当超出索引范围时,抛出一个IndexError异常。

    lt[0] = 6   #lt结果为:  [6, 2, 3, 4, 5]

    lt[9]
    
    Traceback (most recent call last):
      File "<pyshell#4>", line 1, in <module>
        lt[9]
    IndexError: list index out of range
    

    二、list的属性和方法

    1.append方法是向列表追加一个元素,返回值是None

    lt.append(8)  

    2.insert方法是在指定索引处插入一个元素,如果指定的正索引值超过索引范围就在末尾追加元素。如果指定的负索引在索引范围就从右向左的对应位置,但指定的负索引不在索引范围就在列表的0索引位置插入一个元素。

    lt = [1, 2, 3, 4, 5]
    lt.insert(40,33)
    lt.insert(-2,88)
    lt.insert(-40,333)
    
    lt列表的结果为:[333, 1, 2, 3, 4, 88, 5, 33]
    

      3.extend方法是把一个列表追加到另外一个列表中

    lt.extend([44, 55, 66])

    4.pop方法是在列表最后删除一个元素,也可以指定索引来删除元素,并返回删除的元素。索引默认为-1,如果index超出索引范围会出了IndexError异常。

    lt.pop()

    lt.pop(33)
    
    Traceback (most recent call last):
      File "<pyshell#31>", line 1, in <module>
        lt.pop(33)
    IndexError: pop index out of range
    

      5.remove方法是删除最近一个相对应的值,如果指定的值不在列表中就抛出一个ValueError异常。

    lt.remove(2)

    lt.remove(98)
    
    Traceback (most recent call last):
      File "<pyshell#34>", line 1, in <module>
        lt.remove(98)
    ValueError: list.remove(x): x not in list
    

      6.clear方法是清除列表中的所有元素,变成一个空列表。

    lt.clear()

    7.index方法通过值查找最近的一个索引,也可以指定查找范围。如果指定的索引不在索引范围内就抛出一个ValueErro异常。

    lt.index(44)

    lt.index(90)
    
    Traceback (most recent call last):
      File "<pyshell#36>", line 1, in <module>
        lt.index(90)
    ValueError: 90 is not in list
    

      8.count方法通过值统计在列表中出现过多少次。如果不存在就返回0

    lt.count(2)

    9.sort方法对列表进行排序操作。也可以指定参数reverse和key,如果reverse=True时为倒排序。

    lt.sort()

    10.reverse()方法把一个列表倒排。

    lt.reverse()

    11.copy方法是复制一个新列表。

    lt2 = lt.copy()

    三、列表的切片操作

     

    lt = list(range(20))
    
    print(lt[0:8])
    #结果:[0, 1, 2, 3, 4, 5, 6, 7]
    
    print(lt[-8:-2])
    #结果:[12, 13, 14, 15, 16, 17]
    
     print(lt[-8:-5])
    #结果:[12, 13, 14]
    
    #注意 切片操作总是从左向右,所以左边要小于右边,反知得到一个空列表。左边超出索引范围从0开始,右边超出索引范围取到
    列表最后一个元素。

     

    print(lt[:12])
    #结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    
    print(lt[2:])
    #结果:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

     

    print(lt[::2])
    #结果:[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

     print(lt[::-1])   

    #结果:[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    #可以为切片设置步长。当步长为负数时,从后往前,此时应该左边大于右边,否则返回一个空列表。
    
    lt = list(range(10))
    
    lt[3:5] = ['a', 'b']
    print(lt)
    #结果:[0, 1, 2, 'a', 'b', 5, 6, 7, 8, 9]
    
    lt[2:4] = 'x'
    print(lt)
    #结果:[0, 1, 'x', 'b', 5, 6, 7, 8, 9]
    
    lt[2:4] = ['a', 'b', 'c', 'd']
    print(lt)
    #结果:[0, 1, 'a', 'b', 'c', 'd', 5, 6, 7, 8, 9]
    
    #对于切片赋值时,会替换切片原来的元素。如果不连续的切片赋值时,需要有相同个数的值。
    

    四、解包和封包

    1.解包

    把一个列表或者元组,同时赋值给多个变量时,称为解包。

    如果等于号左边的变量个数少于右边的个数时,需要使用带“*”的变量。如果不需要一个值时可以用一个下划线来代替一个变量。如果不需要多于的值时,可以使用带“_"的星号。

    x = 1
    y = 2
    x, y = (y, x)
    print(x, y)
    #结果:2 1

     lt = list(range(20))
     x, *y = lt
     print(x,y)

    #结果:0 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

     x, *_, y = lt
     print(x, y)

    #结果:0 19

     lt = [1,[2,3,4,5],6]
     a,(b,*_,c),d=lt
     print(a,b,c,d)

    #结果:1 2 5 6
    #只要等于号左边和右边的结构相同就能赋值成功。

    2.封包

    把多个变量,同时赋值给一个变量构建成一个元组时,称为封包。

    t = x , y
    print(type(t))
    #结果:<class 'tuple'>
    

      

  • 相关阅读:
    搜索框的实现
    图片瀑布流实现
    git的基本操作总结
    linux中常用命令总结
    JavaScript中的闭包
    springmvc执行过程
    位运算
    MySQL与Oracle数据库连接配置
    java 基础数据类型大小
    spring源码编译控制台输出乱码
  • 原文地址:https://www.cnblogs.com/orna/p/8276973.html
Copyright © 2011-2022 走看看