python中删除列表元素

 

>>> test = ["aaa","bbb","ccc","ddd"]
>>> type(test)
<class 'list'>
>>> len(test)
4
>>> test.pop()   ## pop弹出
'ddd'
>>> test
['aaa', 'bbb', 'ccc']
>>> test.pop(1)
'bbb'
>>> test
['aaa', 'ccc']
>>> test.insert(1,"bbb")
>>> test
['aaa', 'bbb', 'ccc']
>>> test.append("ddd")
>>> test
['aaa', 'bbb', 'ccc', 'ddd']
>>> test.extend(["eee","fff"])
>>> test
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
>>> del test[1]
>>> test
['aaa', 'ccc', 'ddd', 'eee', 'fff']
>>> del test[0]    ## del 删除
>>> test
['ccc', 'ddd', 'eee', 'fff']
>>> del test[-1]
>>> test
['ccc', 'ddd', 'eee']
>>> del test
>>> test
Traceback (most recent call last):
  File "<pyshell#602>", line 1, in <module>
    test
NameError: name 'test' is not defined
>>> test = ["aaa","bbb","ccc","ddd"]
>>> test
['aaa', 'bbb', 'ccc', 'ddd']
>>> test.remove("bbb")  ## remove删除
>>> test
['aaa', 'ccc', 'ddd']
>>> test.remove("aaa")
>>> test
['ccc', 'ddd']
>>> test + test
['ccc', 'ddd', 'ccc', 'ddd']
>>> test1 = test + test
>>> test1
['ccc', 'ddd', 'ccc', 'ddd']
>>> test1 = test1 + test1
>>> test1
['ccc', 'ddd', 'ccc', 'ddd', 'ccc', 'ddd', 'ccc', 'ddd']
>>> test1.remove("ccc")
>>> test1
['ddd', 'ccc', 'ddd', 'ccc', 'ddd', 'ccc', 'ddd']

 

posted @ 2020-12-18 19:51  小鲨鱼2018  阅读(132)  评论(0编辑  收藏  举报