Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
1. Create a list
数组:存储同一种数据类型的集合 scores = [34,56,90,100..]
列表(打了激素的数组):可以存储任意数据类型
"""
list = [1,1.2,True,'westos']
print(list,type(list))
# 列表里面也可以嵌套列表
list2 = [1,1.2,True,'westos',[1,2,3,4]]
print(list2,type(list2))
2. The list of properties
service = ['http','ftp','ssh']
# 索引
# 正向索引
print(service[0])
# 反向索引
print(service[-1])
# 切片
print(service[::-1])
print(service[1:])
print(service[:-1])
# 重复
print(service * 3)
# 连接
service1 = ['mysql','firewalld']
print(service + service1)
# 成员操作符
print('firewalld' in service)
print('ftp' not in service1)
3. List the elements increase
service = ['http','ftp','ssh']
# append():追加一个元素到列表
service.append('firewalld')
# extend():拉伸 追加多个元素到列表
service.extend(['mysql','nfs'])
# insert():在指定索引处插入元素
service.insert(1,'https')
4. Delete list elements
>>> service = ['http','ftp','ssh']
>>> service.pop()
'ssh'
>>> service
['http', 'ftp']
>>> service.pop()
'ftp'
>>> service
['http']
>>> service = ['http','ftp','ssh']
>>> service
['http', 'ftp', 'ssh']
>>> service.pop(0)
'http'
>>> service
['ftp', 'ssh']
>>> service.pop()
'ssh'
>>> service
['ftp']
>>> service.pop()
'ftp'
>>> service
[]
>>> service.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop from empty list
remove (): Delete list element
service = [‘http’,‘ftp’,‘ssh’]
service.remove(‘ftp’)
print(service)Remove an element from memory
del service[1]
print(service)