zoukankan      html  css  js  c++  java
  • python删除list中元素的三种方法

    1. a.pop(index):删除列表a中index处的值,并且返回这个值.
    2. del(a[index]):删除列表a中index处的值,无返回值. del中的index可以是切片,所以可以实现批量删除.
    3. a.remove(value):删除列表a中第一个等于value的值,无返回.
    >>> a = [0, 2, 3, 2]
    >>> a.remove(2)
    >>> a
    [0, 3, 2]
    
    >>> a = [3, 2, 2, 1]
    >>> del a[1]
    >>> a
    [3, 2, 1]
    
    >>> a = [4, 3, 5]
    >>> a.pop(1)
    3
    >>> a
    [4, 5]
    
    #错误信息也不一样
    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
  • 相关阅读:
    多线程 介绍
    AE中如何获取曲线的一部分(转)
    friday
    THU
    MON
    SAT
    周三
    TUE
    绝对遗憾!
    monday
  • 原文地址:https://www.cnblogs.com/zywscq/p/10760232.html
Copyright © 2011-2022 走看看