1.列表
1)创建列表
数组:存储同一种数据类型的集合 scores=[12,13,14]
列表:(打了激素的数组):可以存储任意数据类型的集合
列表里:可以存储不同的数据类型
s=[1,4,5,'ty']
print s,type(s)
列表里面也可以嵌套列表(列表:也是一种数据类型)
s=[1,4,2,[1,4,'aa']]
print s,type(s)
2)列表的特性
# # 索引
# 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 'firewalld' in service1
# print 'firewalld' not in service
# for循环遍历
# print '显示服务'.center(50,'*')
# for se in service:
# print se
# 列表里嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
# 索引
print service2[0][1]
print service2[-1][1]
# 切片
print service2[:][1]
print service2[:-1][0]
print service2[0][:-1]
假定有下面这样的列表:
names = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’]
输出结果为:’I have fentiao, fendai, fensi and apple.’
names = ['fentiao', 'fendai', 'fensi', 'apple']
print 'I have ' + ','.join(names[:-1]) + ' and ' + names[-1]
3)列表的查看
service = ['http', 'ssh', 'ftp','ftp']
# 查看列表中元素出现的次数
print service.count('ssh')
# 查看指定元素的索引值
print service.index('ssh')
4)列表的增加
# 1.
print service + ['firewalld'