列表
列表(打了激素的数组):可以存取任意数据类型
列表中可以存取列表
service = ["https","ftp","ssh"]
#索引
#正向索引
print(service[0])
#反向索引
print(service[-1])
#切片
print(service[::-1])
print(service[1:])
print(service[:-1])
#重复
print(service * 3)
#连接
servicel = ["mysql"+"python"]
print(service+servicel)
#成员操作符
print("mysql" in servicel)
print("mysql" not in servicel)
https
ssh
['ssh', 'ftp', 'https']
['ftp', 'ssh']
['https', 'ftp']
['https', 'ftp', 'ssh', 'https', 'ftp', 'ssh', 'https', 'ftp', 'ssh']
['https', 'ftp', 'ssh', 'mysqlpython']
False
True
service = ["https","ftp","ssh"]
#append():追加一个元素到列表
service.append("fileallld")
print(service)
#extend():追加多个元素到列表
service.append(['mysql','index'])
print(service)
#insert()在指定的索引位置之前插入数据
service.insert(1,'hh')
print(service)
['https', 'ftp', 'ssh', 'fileallld']
['https', 'ftp', 'ssh', 'fileallld', ['mysql', 'index']]
['https', 'hh', 'ftp', 'ssh', 'fileallld', ['mysql', 'index']]
列表.pop #弹出一个元素并使用它,可以用来赋值给其他变量
列表.remove #则是直接移除该元素
del 列表[下标] #从内存地址中删除
列表.count("元素") #查看元素在该列表中出现的次数
列表.index("元素") #查看对应元素中的下标(可以指定索引值的范围)
#默认按照ASCII码进行排序
列表.sort() #正序排序
列表.sort(reverse=Ture) #逆序排序
列表.shuffle #随意打乱列表顺序
列表也可以通过索引值来赋值
service = ["https","ftp","ssh"]
#通过索引值来赋值
service[0] = 'sa'
print(service)
#通过切片来赋予一段值
print(service[:2])
service[:2] = ["ab","hk"]
print(service)
['sa', 'ftp', 'ssh']
['sa', 'ftp']
['ab', 'hk', 'ssh']