美文网首页
Python 3 笔记 - 第2章 数据结构 - List 列表

Python 3 笔记 - 第2章 数据结构 - List 列表

作者: 小叶与小茶 | 来源:发表于2018-06-22 11:52 被阅读27次

    List 是使用最频繁的一种序列类数据结构,元素写在方扣号(“[]”)里,用逗号相间隔。

    1. List 是一个超级收纳箱,其中的元素的数据类型可以不同。

    super_box = [1, 2.0, 'hello', 'abc', 500]
    print(super_box)
    

    执行结果为:

    [1, 2.0, 'hello', 'abc', 500]
    

    2. 和字符串类似,列表也可以进行截取、连接和重复。

    super_box = [1, 2, 3, 4, 5, 6, 7, 8]
    super_box_little = [9, 10]
    
    print(super_box)                       # 输出完整列表
    print(super_box[0])                    # 输出第一个元素
    print(super_box[2])                    # 输出第三个元素
    print(super_box[0:])                   # 输出从第一个元素到最后一个元素
    print(super_box[:-1])                  # 输出从第一个元素到倒数第二个元素
    print(super_box[1:4])                  # 输出从第二哥元素到第四个元素
    print(super_box * 2)                   # 输出两次列表
    print(super_box + super_box_little)    # 输出连接后的列表
    

    执行结果为:

    [1, 2, 3, 4, 5, 6, 7, 8]
    1
    3
    [1, 2, 3, 4, 5, 6, 7, 8]
    [1, 2, 3, 4, 5, 6, 7]
    [2, 3, 4]
    [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    3. 和字符串不同的是,List 中的元素是可以改变的!

    super_box = [1, 2, 2, 4, 5] 
    super_box[0] = 5
    
    print(super_box)
    

    执行结果为:

    [5, 2, 2, 4, 5]
    

    4. 这样进行初始化是不对的。

    super_box = []
    super_box[0] = 1
    super_box[1] = 2
    super_box[2] = 3
    

    执行结果为:

    IndexError: list assignment index out of range
    

    要么一开始就知道列表的大小和元素,直接赋值

    super_box = [1 , 2, 3]
    

    如果不知道,就调用 append() 或 insert() 方法逐一添加元素

    5. 使用 append() 或 insert() 添加元素。

    # 使用 append 方法在列表尾部添加元素
    super_box = []
    
    super_box.append(1)    # 在列表尾部添加
    super_box.append(2)    # 在列表尾部添加
    super_box.append(3)    # 在列表尾部添加
    
    print(super_box)
    

    执行结果为:

    [1, 2, 3]
    
    ********************************************
    
    #使用 insert 方法在指定位置上添加元素
    super_box = []
    
    super_box.insert(0, 1)    # 在第一个位置添加
    super_box.insert(1, 2)    # 在第二个位置添加
    super_box.insert(2, 3)    # 在第三个位置添加
    
    print(super_box)
    

    执行结果为:

    [1, 2, 3]
    

    6. 使用 del 语句、pop() 或 remove() 删除元素。

    del 语句

    # 使用 del 语句删除指定位置的元素
    super_box = [1, 2, 3]
    
    del super_box[0]
    
    print(super_box)
    

    执行结果为:

    [2, 3]
    

    不指定索引的 pop() 方法

    # 不输入元素位置,pop() 默认弹出最顶端(最后一个)列表元素
    super_box = [1, 2, 3]
    
    super_box.pop()
    print(super_box)
    
    super_box.pop()
    print(super_box)
    

    执行结果为:

    [1, 2]
    [1]
    

    指定索引的 pop() 方法

    # 使用 pop 语句弹出指定位置的元素
    super_box = [1, 2, 3]
    
    super_box.pop(0) 
    
    print(super_box)
    

    执行结果为:

    [2, 3]
    

    remove() 方法

    # 使用 remove() 删除指定的元素
    super_box = [1, 2, 3]
    
    super_box.remove(1)
    
    print(super_box)
    

    执行结果为:

    [2, 3]
    

    针对 pop() 和 remove() 两个方法,有两个小细节说一下:

    • pop() 的参数是元素位置,remove() 的参数是元素内容,不能弄混,我们体会一下

    remove() 的入参为欲删除的元素位置

    super_box = [1, 2, 3]
    super_box.remove(0)
    

    执行结果为:

    super_box.remove(0)
    ValueError: list.remove(x): x not in list
    

    pop() 的入参为欲删除的元素

    super_box = [1, 2, 3]
    super_box.pop(3)
    

    执行结果为:

    super_box.pop(3)
    IndexError: pop index out of range
    
    • 调用 pop() 后其返回值就是弹出的元素,而调用 remove() 后则无法再利用其返回值
    # pop() 的返回值
    super_box = [1, 2, 3]
    a = super_box.pop(0)
    print("调用 pop 方法后还可以将弹出的元素赋给一个变量:a = " + str(a))
    
    # remove() 的返回值
    super_box = [1, 2, 3]
    a = super_box.remove(1)
    print("调用 remove 方法是无法获得删除的元素的:a = " + str(a))
    

    执行结果为:

    调用 pop 方法后还可以将弹出的元素赋给一个变量:a = 1
    调用 remove 方法是无法获得删除的元素的:a = None
    

    7. 遍历列表。

    如果使用 Java ,大概就是这么遍历列表的

    super_box = [1, 2, 3];
    
    for (int i = 0; i < super_box.length(); i++) {
        print(super_box[i]);
    }
    

    而 Python 呢,更简洁一些

    super_box = [1, 2, 3]
    
    for element in super_box :
        print(element)
    

    执行结果为:

    1
    2
    3
    

    Python 中取消了花括号,并使用 “for element in elements” 这种更贴合自然语言的句式,后面我们还能看到 Python 有很多这样的句式。这些都让 Python 看起来更加简洁。

    8. 列表嵌套。

    列表是可以通过嵌套成为多维列表。

    # 列表嵌套
    super_box_1 = [1, 2, 3]
    super_box_2 = ['a', 'b', 'c']
    super_box   = [super_box_1, super_box_2]
    
    print(super_box)        # 输出完整嵌套列表
    print(super_box[0])     # 输出嵌套列表第一个元素
    print(super_box[1])     # 输出嵌套列表第二个元素
    print(super_box[0][0])  # 输出嵌套列表第一个元素的第一个子元素
    print(super_box[1][1])  # 输出嵌套列表第二个元素的第二个子元素
    

    执行结果为:

    [[1, 2, 3], ['a', 'b', 'c']]
    [1, 2, 3]
    ['a', 'b', 'c']
    1
    b
    

    9. 列表相关的函数和方法

    这里说的函数值得是 Python 内置的、可供全局调用的方法;而方法则指的是列表自身提供的方法。

    序号 类别 名称 说明
    1 len() 全局函数 len(list) 返回列表元素个数
    2 min() 全局函数 min(list) 返回列表中元素最小值
    3 max() 全局函数 max(list) 返回列表中元素最大值
    4 append() 方法 list.append(obj) 在列表尾部添加元素
    5 insert() 方法 list.insert(position, obj) 在列表指定位置添加元素
    6 pop() 方法 list.pop(position) 弹出列表指定位置元素
    7 remove() 方法 list.remove(element) 删除列表指定元素
    8 reverse() 方法 list.reverse() 将列表元素进行反转
    9 clear() 方法 list.clear() 清空列表
    10 copy() 方法 list.copy() 复制列表
    super_box   = [1, 2, 3, 4, 5, 6, 7, 8]
    
    print("len(super_box) = " + str(len(super_box)))
    print("min(super_box) = " + str(min(super_box)))
    print("max(super_box) = " + str(max(super_box)))
    
    super_box_copy = super_box.copy()
    print("super_box.copy() = " + str(super_box_copy))
    
    super_box.reverse()
    print("super_box.reverse() = " + str(super_box))
    
    super_box.clear()
    print("super_box.clear() = " + str(super_box))
    

    执行结果为:

    len(super_box) = 8
    min(super_box) = 1
    max(super_box) = 8
    super_box.copy() = [1, 2, 3, 4, 5, 6, 7, 8]
    super_box.reverse() = [8, 7, 6, 5, 4, 3, 2, 1]
    super_box.clear() = []
    

    相关文章

      网友评论

          本文标题:Python 3 笔记 - 第2章 数据结构 - List 列表

          本文链接:https://www.haomeiwen.com/subject/wdkkyftx.html