一.创建列表
数组:存储同一种数据类型的集合 scores = [1,2,3]
列表:可以存储任意数据类型的集合
简单的列表创建:
In [1]: name1 = 'tom'
In [2]: name2 = 'Tony'
In [3]: name3 = 'coco'
In [4]: name1
Out[4]: 'tom'
In [5]: name2
Out[5]: 'Tony'
In [6]: name3
Out[6]: 'coco'
In [7]:
In [7]: name = ['tom','Tony','coco']
In [8]:
In [8]: name
Out[8]: ['tom', 'Tony', 'coco']
In [9]: type(name) ##查看数据类型
Out[9]: list
列表可存储不同的数据类型
li = [1,1.2,'hello',True]
print(li)
print(type(li))
列表里嵌套列表(列表本身也是一种数据类型)
li1 = [1,1.2,'hello',True,[1,2,3,4,5]]
print(li1)
print(type(li1))
二.列表的特性
- 索引
service = ['http','ssh','ftp','dns']
print(service[0])
print(service[-1])
- 切片
service = ['http','ssh','ftp','dns']
print(service[1:])
print(service[:-1])
print(service[::-1])
- 重复
service = ['http','ssh','ftp','dns']
print(service * 3)
- 连接
service = ['http','ssh','ftp','dns']
service1 = ['mysql','firewalld']
print(service + service1)
- 成员操作符
service = ['http','ssh','ftp','dns']
service1 = ['mysql','firewalld']
print('mysql' in service)
print('mysql' in service1)
- 迭代
service = ['http','ssh','ftp','dns']
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])
练习题
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])
2.季节的判断
用户输入月份,判断这个月是哪个季节
month = int(input('请输入月份:'))
if month in [3,4,5]:
print('春季')
elif month in [6,7,8,]:
print('夏季')
elif month in [9,10,11]:
print('秋季')
elif month in [12,1,2]:
print('冬季')
else:
print('输入不合法')
三.列表的增加
1.
service = ['http','ssh','ftp','dns']
print(service + ['firewalld'])
2.append:追加 追加一个元素到列表中
service = ['http','ssh','ftp','dns']
service.append('firewalld')
print(service)
3.extend:拉伸 追加多个元素到列表中
service = ['http','ssh','ftp','dns']
service.extend(['mysql','firewalld'])
print(service)
4.insert:在指定索引位置插入元素
service = ['http','ssh','ftp','dns']
service.insert(1,'samba')
print(service)
四.列表的删除
-
.pop:默认拿出顶部元素,加上索引拿出指定位置元素,且.pop是有输出的,输出值可以放在之后的程序中使用!
service = [‘http’,‘ssh’,‘ftp’]
b = service.pop()
a = service.pop(1)
print(service)
print(b)
print(a)结果:
[‘http’]
ftp
ssh -
remove:删除指定元素,无输出*
service = ['http','ssh','ftp']
a = service.remove('ssh')
print(service)
print(a) #无输出,因为remove直接删除指定元素
结果:
['http', 'ftp']
None
- del关键字 从内存中删除
service = ['http','ssh','ftp']
print(service)
del service
print(service)
结果:
Traceback (most recent call last):
File "/home/kiosk/PycharmProjects/hetoto/day05/liebiao.py", line 11, in <module>
print(service)
NameError: name 'service' is not defined
五.列表的修改
- 通过索引,重新赋值
service = ['http','ssh','ftp']
service[0] = 'mysql'
print(service)
结果:
['mysql', 'ssh', 'ftp']
- 通过切片,重新赋值
service = ['http','ssh','ftp']
print(service[0:2])
service[0:2] = ['samba','ldap']
print(service)
结果:
['http', 'ssh']
['samba', 'ldap', 'ftp']
六.列表的查看
- 查看出现的次数
service = ['ftp','http','ssh','ftp']
print(service.count('ftp'))
结果:
2
- 查看指定元素的索引值(可以指定索引范围查看)
service = ['ftp', 'http', 'ssh', 'ftp']
print(service.index('ssh'))
print(service.index('ftp', 0, 3))
结果:
2
0
七.列表排序
In [24]: names = ['alice','bob','harry','Borry']
In [25]: names.sort() ##默认是先大写在小写的顺序
In [26]: names
Out[26]: ['Borry', 'alice', 'bob', 'harry']
In [27]: names.sort(key=str.lower) ##以小写进行排序
In [28]: names
Out[28]: ['alice', 'bob', 'Borry', 'harry']
In [29]: names.sort(key=str.upper) ##以大写进行排序
In [30]: names
Out[30]: ['alice', 'bob', 'Borry', 'harry']
- 将原有的列表顺序打乱
import random
li = list(range(10))
print(li)
#将原有的列表打乱
random.shuffle(li)
print(li)
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 5, 7, 9, 8, 6, 1, 3, 4, 0]