zoukankan      html  css  js  c++  java
  • python中从列表中删除元素

    python中从列表中删除元素

    remove:删除指定元素

    pop:删除特定索引位置元素

    del:删除变量

    1、remove

    >>> test1 = ["aa","bb","cc","dd","ee","ff","gg"]
    >>> test1
    ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg']
    >>> test1.remove("aa")
    >>> test1
    ['bb', 'cc', 'dd', 'ee', 'ff', 'gg']
    >>> test1.remove("dd")
    >>> test1
    ['bb', 'cc', 'ee', 'ff', 'gg']

    2、pop

    >>> test1
    ['bb', 'cc', 'ee', 'ff', 'gg']
    >>> test1.pop()
    'gg'
    >>> test1
    ['bb', 'cc', 'ee', 'ff']
    >>> test1.pop(2)
    'ee'
    >>> test1
    ['bb', 'cc', 'ff']

    3、del

    >>> test1
    ['bb', 'cc', 'ff']
    >>> del test1[1]
    >>> test1
    ['bb', 'ff']
    >>> del test1[0]
    >>> test1
    ['ff']
    >>> del test1
    >>> test1
    Traceback (most recent call last):
      File "<pyshell#259>", line 1, in <module>
        test1
    NameError: name 'test1' is not defined
  • 相关阅读:
    [SCOI2005]栅栏
    状压dp常用操作
    [SCOI2005]互不侵犯
    欧拉函数
    hdu5179 beautiful number
    hdu4460 Friend Chains
    exgcd详解
    hdu6468 zyb的面试
    hdu1978 How many ways
    hdu1312 Red and Black
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14715536.html
Copyright © 2011-2022 走看看