数组:存储同一种数据类型的集合 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
In [10]:
"""
#列表里:可以存储不同的数据类型
li = [1,1.2,True,'hello']
print(li)
print(type(li))
#列表里也可以嵌套列表(列表:本身也是一种数据类型)
li1 = [1,1.2,True,'hello',[1,2,3,4,5]]
print(li1)
print(type(li1))
二、列表的特性
service=[‘http’,‘ssh’,‘ftp’]
1.索引
print(service[0])
print(service[-1])
2.切片
print(service[1:])
print(service[:-1])
print(service[::-1])
3.重复
print(service * 3)
4.连接
service1 = [‘mysql’,‘firewalld’]
print(service + service1)
5.成员操作符
print(‘firewalld’ in service)
print(‘firewalld’ in service1)
6.for循环遍历
for se in service:
print(se)
三、列表里嵌套列表
service2 = [[‘http’,80],[‘ssh’,22],[‘ftp’,21]]
1.索引
print(service2[1][1])
print(service2[-1][1])
2.切片
print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])
四、列表的增加
service = [‘http’,‘ssh’,‘ftp’]
1.
print(service + [‘firewalld’])
2.append:追加一个元素到列表中
service.append(‘firewalld’)
print(service)
3.extend:拉伸 追加多个元素到列表中
service.extend([‘mysql’,‘firewalld’])
print(service)
4.insert:在指定索引位置插入元素
service.insert(1,‘samba’)
print(service)
五、列表的删除
1.pop
In [12]: service = [‘http’,‘ssh’,‘ftp’]
In [13]:
In [13]: service.pop()
Out[13]: ‘ftp’
In [14]: service
Out[14]: [‘http’, ‘ssh’]
In [15]: service.pop()
Out[15]: ‘ssh’
In [16]: service
Out[16]: [‘http’]
In [17]: service.pop()
Out[17]: ‘http’
In [18]: service
Out[18]: []
In [19]: service.pop()
IndexError Traceback (most recent call last)
in
----> 1 service.pop()
IndexError: pop from empty list
“”"
service = [‘http’,‘ssh’,‘ftp’]
2.remove:删除指定元素
a = service.remove(‘ssh’)
print(service)
print(a)
3.del关键字 从内存中删除
print(service)
del service
print(service)
六、列表的修改
service = [‘http’,‘ssh’,‘ftp’]
1.通过索引,重新赋值
service[0] = ‘mysql’
print(service)
2.通过切片
print(service[:2])
service[:2] = [‘samba’,‘ldap’]
print(service)
七、列表的查看
service = [‘ftp’,‘http’,‘ssh’,‘ftp’]
#查看出现的次数
print(service.count(‘ftp’))
#查看指定元素的索引值(可以指定索引范围查看)
print(service.index(‘ssh’))
print(service.index(‘ftp’,0,3))
八、排序
service = [‘ftp’,‘http’,‘ssh’,‘ftp’]
service.sort()
print(service)
import random
“”"
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’]
In [31]:
“”"
li = list(range(10))
print(li)
#将原有的列表顺序打乱
random.shuffle(li)
print(li)
九、练习题
“”"
1.用户输入月份,判断这个月是哪个季节
2.假定有下面的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘apple’]
输出结果为:‘I have fentiao, fendai, fensi and apple.’
“”"
# month = int(input('Month:'))
# 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('输入不合法')
names = ['fentiao','fendai','fensi','apple']
print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])