(1)列表的增加(指的是给列表中添加元素)
方式一:直接添加
service = ['http','ssh','ftp']
print(service + ['firewalld'])
方式二:append:追加,追加一个元素到列表中
service = ['http','ssh','ftp']
service.append('firewalld')
print(service)
方式三:extend:拉伸 追加多个元素到列表中
service = ['http','ssh','ftp']
service.extend(['mysql','firewalld'])
print(service)
方式四:insert:在指定索引位置插入元素
service = ['http','ssh','ftp']
service.insert(1,'samba')
print(service)
(2)列表的删除(删除列表中的一个或者多个元素或者整个列表)
方式一:remove:删除指定的元素
service = ['http','ssh','ftp']
a = service.remove('ssh')
print(service)
方式二:del:直接从内存中删除
service = ['http','ssh','ftp']
print(service)
del service
print(service)
(3)列表的修改(修改列表中某些元素的值)
方式一:通过索引 重新赋值
service = ['http','ssh','ftp']
service[0] = 'mysql'
print(service)
方式二:通过切片 重新赋值
service = ['http','ssh','ftp']
print(service[:2])
service[:2] = ['samba','iscsi']
print(service)
(4)列表的查看(指定查看列表的一些信息)
方式一:查看出现的次数
service = ['ssh','http','ssh','ftp']
print(service.count('ssh'))
方式二:查看指定元素的索引值
service = ['ssh','http','ssh','ftp']
print(service.index('ssh'))
print(service.index('ssh',0,3))