一.添加
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
二.insert添加
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
三.extend添加
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.append(['d','e'])
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', ['d', 'e']] #发现并不是理想结果
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
四.删除remove
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
#remove
team.remove('d') #要加删除的内容
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
['a', 'c', 'b', 'e']
五.pop()删除
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
#remove
team.remove('d')
print(team)
#pop 删除尾部的元素,并返回删除的元素
print("删除的元素:",team.pop())
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
['a', 'c', 'b', 'e']
删除的元素: e
['a', 'c', 'b']
六.pop(索引)
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
#remove
team.remove('d')
print(team)
#pop 删除尾部的元素,并返回删除的元素
print("删除的元素:",team.pop())
print(team)
#pop(索引)
print("删除的元素:",team.pop(2))
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
['a', 'c', 'b', 'e']
删除的元素: e
['a', 'c', 'b']
删除的元素: b
['a', 'c']
七.del 删除
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
#remove
team.remove('d')
print(team)
#pop 删除尾部的元素,并返回删除的元素
print("删除的元素:",team.pop())
print(team)
#pop(索引)
print("删除的元素:",team.pop(2))
print(team)
#del 删除
del team[0]
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
['a', 'c', 'b', 'e']
删除的元素: e
['a', 'c', 'b']
删除的元素: b
['a', 'c']
['c']
八.clear清除内容
[root@rhel8 day04]# vim demo03.py
team = ['a']
#添加
team.append('b')
print(team)
#insert 添加
team.insert(1,"c")
print(team)
#extend 添加
team.extend(['d','e'])
print(team)
#remove
team.remove('d')
print(team)
#pop 删除尾部的元素,并返回删除的元素
print("删除的元素:",team.pop())
print(team)
#pop(索引)
print("删除的元素:",team.pop(2))
print(team)
#del 删除
del team[0]
print(team)
#clear
team.clear()
print(team)
[root@rhel8 day04]# python3 demo03.py
['a', 'b']
['a', 'c', 'b']
['a', 'c', 'b', 'd', 'e']
['a', 'c', 'b', 'e']
删除的元素: e
['a', 'c', 'b']
删除的元素: b
['a', 'c']
['c']
[]
文章来源地址https://www.toymoban.com/news/detail-754733.html
文章来源:https://www.toymoban.com/news/detail-754733.html
到了这里,关于python的列表list的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!