zoukankan      html  css  js  c++  java
  • 列表及元组

    列表

    列表(list)是python里的一种常用的数据类型,用于存储经常需要改变的数据,列表有很多方法,下面一一介绍

    1. 切片

    列表的切片与字符串切片基本一致

    1 l1 = [1, 2, 3, "hello", "world", 2]
    2 print(l1[2:6:2])

    输出

    [3, 'world']
    
    Process finished with exit code 0

    列表增加元素的方法有追加、插入和迭代追加三种:

    2. 追加(append)

    在列表尾部添加,可以添加数字,字符串,列表,元组等

    1 l1 = [1, 2, 3, "hello", "world", 2]
    2 l1.append("hellokitty")
    3 print(l1)
    4 l1.append([4, 5, 6])
    5 print(l1)
    6 l1.append(("a", "b", "c"))
    7 print(l1)

    输出

    [1, 2, 3, 'hello', 'world', 2, 'hellokitty']
    [1, 2, 3, 'hello', 'world', 2, 'hellokitty', [4, 5, 6]]
    [1, 2, 3, 'hello', 'world', 2, 'hellokitty', [4, 5, 6], ('a', 'b', 'c')]
    
    Process finished with exit code 0

    3. 插入(insert)

    插入的语法为l1.insert(index, object),意为在指定索引的前面插入对象,插入点后面的元素整体后移

    1 l1.insert(3, "love")
    2 l1.insert(5, [1, 2, 3])
    3 print(l1)

    运行

    [1, 2, 3, 'love', 'hello', [1, 2, 3], 'world', 2]
    
    Process finished with exit code 0

     对于空列表的插入,index可以为任意值

    4. 迭代追加(extend)

    把可迭代变量逐个添加到列表的尾部

    1 l1 = [1, 2, 3, "hello", "world", 2]
    2 l1.extend("abc")
    3 l1.extend(["A", "B", "C"])
    4 print(l1)

    输出

    1 [1, 2, 3, 'hello', 'world', 2, 'a', 'b', 'c', 'A', 'B', 'C']
    2 
    3 Process finished with exit code 0

    删除元素的方法有pop、del、remove和clear四种

    5. pop删除

    按照索引删除元素,并返回删除的值,可以传参,参数为索引,默认索引为last,只有pop有返回值!!!

    1 l1 = [1, 2, 3, 4, ["a", "b"]]
    2 l1.pop(1)
    3 print(l1)
    4 l1.pop(3)
    5 print(l1)

    输出

    [1, 3, 4, ['a', 'b']]
    [1, 3, 4]
    
    Process finished with exit code 0

    由于pop的接收的参数是index,所以pop只能删除列表里最外层的元素。对嵌套的列表里的元素无能为力,删除嵌套列表里的元素需要用到下面的del

    6. del删除

    del有三种删除的方式

    (1)按照索引删除

    1 l1 = [1, 2, 3, 4, ["a", "b"]]
    2 del l1[4][1]
    3 print(l1)

    输出

    [1, 2, 3, 4, ['a']]
    
    Process finished with exit code 0

    (2)按照切片删除

    1 l1 = [1, 2, 3, 4, ["a", "b"]]
    2 del l1[2:5]
    3 print(l1)

    输出

    [1, 2]
    
    Process finished with exit code 0

    细心的朋友们可能发现了,切片的结束索引超出了列表的最大索引居然没有报错!为了验证,我们再来试一下

    1 l1 = [1, 2, 3, 4, ["a", "b"], 5, 6]
    2 
    3 print(l1[5:10])

    输出

    [5, 6]
    
    Process finished with exit code 0

    确实可以,没有报错!既然列表可以,那么字符串和元组可不可以呢,来看看

    1 s = "love"
    2 print(s[1:8])
    3 tu = (1, 2, 3, 4)
    4 print(tu[2:10])

    输出

    ove
    (3, 4)
    
    Process finished with exit code 0

    可以看到对于列表、字符串和元组的切片,结束索引都可以超出被切片元素的最大索引而不会报错,系统默认切到最后一个。

    (3)按照切片和步长删除

    1 l1 = [1, 2, 3, 4, ["a", "b"], 5, 6]
    2 del l1[1:5:2]
    3 print(l1)

    输出

    [1, 3, ['a', 'b'], 5, 6]
    
    Process finished with exit code 0

     

    7. remove

    按照值删除第一个指定的元素

    1 l1 = [1, 2, 3, 4, ["a", "b"], 2]
    2 l1.remove(2)
    3 print(l1)

    输出

    [1, 3, 4, ['a', 'b'], 2]
    
    Process finished with exit code 0

     

    8. clear

    全部清空

    1 l1 = [1, 2, 3, 4, ["a", "b"]]
    2 l1.clear()
    3 print(l1)

    输出

    []
    
    Process finished with exit code 0

    列表元素的修改都是通过赋值来实现的,有三种方式

    9. 按照索引改

    1 l1 = [1, 2, 3, 4, ["a", "b"], 5, 6]
    2 l1[1] = "s"
    3 print(l1)

    输出

    [1, 's', 3, 4, ['a', 'b'], 5, 6]
    
    Process finished with exit code 0

    10. 切片(可以加步长)改

    1 l1 = [1, 2, 3, 4, ["a", "b"], 5, 6]
    2 l1[1:3] = "hello"
    3 print(l1)
    4 l2 = [1, 2, 3, 4, ["a", "b"], 5, 6]
    5 l2[1:5:2] = "sb"
    6 print(l2)

    输出

    [1, 'h', 'e', 'l', 'l', 'o', 4, ['a', 'b'], 5, 6]
    [1, 's', 3, 'b', ['a', 'b'], 5, 6]
    
    Process finished with exit code 0

    注意按照切片修改,不加步长时可以写入任意数量的元素,加上步长时只能写入与切片的元素个数相同的元素

    11.查(index)

    返回指定元素的第一个索引值,找不到报错

    1 l1 = [1, 2, 3, "python", "hello", 2, [5, 2, 6]]
    2 print(l1.index(2))
    3 print(l1.index(2, 3, 7))

    输出

    1
    5
    
    Process finished with exit code 0

    index一次只能获取一个索引,如果想一次获取多个相同元素的索引,可以用enumerate

    1 l1 = [1, 2, 3, "python", "hello", 2, [5, 2, 6]]
    2 for i, j in enumerate(l1):   # 只能找到列表的第一层
    3     if j == 2:
    4         print(i)

    输出

    1
    5
    
    Process finished with exit code 0

    12. 排序(sort)

    一般对数字列表排序才有意义

    1 l1 = [8, 3, 5, 12, 96, 23, 18]
    2 l1.sort(reverse=True)  # reverse=True(可选)表示按降序排列,默认升序
    3 print(l1)

    输出

    [96, 23, 18, 12, 8, 5, 3]
    
    Process finished with exit code 0

    对于字母排序,排序规则就是ASCII码表上的顺序

    1 l1 = ["dog", "apple", "c", "fly"]
    2 l1.sort()
    3 print(l1)

    输出

    ['apple', 'c', 'dog', 'fly']
    
    Process finished with exit code 0

    python3.6不支持数字和字符混合排序

    1 l1 = [8, 3, 5, 12, "a", "c", 18]
    2 l1.sort()
    3 print(l1)

    输出

    Traceback (most recent call last):
      File "D:/列表.py", line 115, in <module>
        l1.sort()
    TypeError: '<' not supported between instances of 'str' and 'int'
    
    Process finished with exit code 1

    13. 反转(reverse)

    l1 = [8, 3, 5, 12, "a", "c", 18]
    l1.reverse()
    print(l1)

    输出

    l1 = [8, 3, 5, 12, "a", "c", 18]
    l1.reverse()
    print(l1)

    这里补充一下列表的运算,列表有两种运算,加和乘

    列表与列表相加

    1 l1 = [1, 2, 3]
    2 l2 = ["a", "b", "c"]
    3 print(l1 + l2)

    输出

    [1, 2, 3, 'a', 'b', 'c']
    
    Process finished with exit code 0

    列表与数字相乘

    1 l1 = [1, 2, 3]
    2 print(l1*3)

    输出

    [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    Process finished with exit code 0

    注意:列表不能与列表相乘!

    好了,列表的方法就是这些了,好累.......我们来总结一下吧~

    (1) 列表方法可以分为切片、增、删、改、查、排序这几类

    (2) 增有append、insert、extend三种方法;删有pop、remove、del、clear四种方法

    (3) 改有按照索引改和切片改(可加步长)两种方法;查有index和enumerate两种方法

    (4) 列表之间可以相加,相加的结果就是把列表拼接起来;列表可以与数字相乘,相乘的结果是列表的元素重复n遍

     

    元组

    元组与列表类似,区别在于元组是不可变数据类型,但是这里有一点需要注意,元组虽然不可变,但是如果元组的元素是列表等可变数据类型时那么元组的元素是可以改变的,说起来有点拗口,看完代码里就明白了

    1 tu = (1, 2, 3, ["a", "b", "c"])
    2 tu[1] = 20
    3 print(tu)

    输出报错

    Traceback (most recent call last):
      File "D:/元组.py", line 35, in <module>
        tu[1] = 20
    TypeError: 'tuple' object does not support item assignment
    
    Process finished with exit code 1

    对元组的可变元素就行修改

    1 tu = (1, 2, 3, ["a", "b", "c"])
    2 tu[3][0] = tu[3][0].upper()
    3 print(tu)

    输出

    (1, 2, 3, ['A', 'b', 'c'])
    
    Process finished with exit code 0

    元组的方法与列表基本相同,这里就不一一介绍了

  • 相关阅读:
    Resource interpreted as Stylesheet but transferred with MIME type application/x-css
    scrapy 的settings.py中设置自定义属性
    scrapy下载图片到指定路径
    scrapy 自动下载图片
    Python处理json的错误 obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 2 column 17 (char 18)
    [ZJOI2012]数列 题解 [高精]
    [ZJOI2020]传统艺能 题解 [DP+矩阵+分类讨论]
    《终将成为你》简评
    GJGHFD的关键点 题解 [倍增+线段树+贪心]
    GJGHFD的排列 题解 [DP]
  • 原文地址:https://www.cnblogs.com/zzliu/p/10171368.html
Copyright © 2011-2022 走看看