数组:存储同一数据类型的集合 score = [10,20,30]
列表:可以存储任意数据类型的集合,利用一个变量来存储多个类型的信息
1.创建列表:
1>列表里可以存储不同的数据类型
li = [1,1.2,'hello',True]
print(li)
print(type(li))
测试结果:
[1,1.2,'hello',True]
<class 'list'>
2>列表嵌套
li1 = [1,1.2,'hello',True,[1,2,3,4,5]]
print(li1)
print(type(li1))
测试结果:
[1,1.2,'hello',True,[1,2,3,4,5]]
<class 'list'>
2.列表的特性
service = [‘http’,‘ssh’,‘ftp’,‘dns’]
(1)索引
service = ['http','ssh','ftp','dns']
print(service[0])
print(service[-1])
测试结果:
http
dns
(2)切片
service = ['http','ssh','ftp','dns']
print(service[1:])
print(service[:-1])
print(service[::-1])
测试结果:
['ssh', 'ftp', 'dns']
['http', 'ssh', 'ftp']
['dns', 'ftp', 'ssh', 'http']
(3)重复
service = ['http','ssh','ftp','dns']
print(service * 2)
测试结果:
['http', 'ssh', 'ftp', 'dns', 'http', 'ssh', 'ftp', 'dns']
(4)连接
service = ['http','ssh','ftp','dns']
service1 = ['mysql','firewall']
print(service + service1)
测试结果:
['http', 'ssh', 'ftp', 'dns', 'mysql', 'firewall']
(5)成员操作符
service = ['http','ssh','ftp','dns']
print('mysql' in service)
print('mysql' in service1 )
测试结果:
False
True
(6)迭代
service = ['http','ssh','ftp','dns']
print('显示所有服务'.center(30,'*'))
for se in service:
print(se)
测试结果:
************显示所有服务************
http
ssh
ftp
dns
3.列表里嵌套列表
service2 = [[‘http’,80],[‘ssh’,22],[‘ftp’,21]]
(1)索引
service2 = [['http',80],['ssh',22],['ftp',21]]
print(service2[0][1])
print(service2[-1][1])
测试结果:
80
21
(2)切片
service2 = [['http',80],['ssh',22],['ftp',21]]
print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])
测试结果:
['ssh', 22]
['http', 80]
['http']
练习:
假定有下面的列表:
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].)
测试结果:
I have fentiao,fendai,fensi and apple.
4.列表的增加
service = [‘http’,‘ssh’,‘ftp’,‘dns’]
(1)使用连接的方式增加
service = ['http','ssh','ftp','dns']
print(service + ['firewalld'])
测试结果:
['http','ssh','ftp','dns','firewalld']
(2)append:追加 追加一个元素到列表中
service = ['http','ssh','ftp','dns']
service.append('firewalld')
print(service)
测试结果:
['http','ssh','ftp','dns','firewalld']
(3)extend:拉伸 追加多个元素到列表中
service = ['http','ssh','ftp','dns']
service.extend(['mysql','firewalld'])
print(service)
测试结果:
['http','ssh','ftp','dns','mysql','firewalld']
(4)insert:在指定索引位置插入元素
service = ['http','ssh','ftp','dns']
service.insert(1,'samba')
print(service)
测试结果:
['http','samba','ssh','ftp','dns']
5.列表的删除
(1)弹出
service = ['http','ssh','ftp','dns']
service.pop()
'dns'
service
['http', 'ssh', 'ftp']
弹出的是最后一个
a = service.pop()
print(service)
['http', 'ssh']
print(a)
'ftp'
弹出到列表为空时再次弹出会报错
(2)remove:删除指定元素
service = ['http','ssh','ftp','dns']
a = service.remove('ssh')
print(service)
['http', 'ftp', 'dns']
print(a) ##a不能再次使用
None
print(service)
['http', 'ftp', 'dns']
(3)del:从内存中删除列表
del service
print(service)
6.列表的修改
service = [‘http’,‘ssh’,‘ftp’,‘dns’]
(1)通过索引,重新赋值
service = ['http','ssh','ftp','dns']
service[0] = 'mysql'
print(service)
测试结果:
['mysql','ssh','ftp','dns']
(2)通过切片
service = ['http','ssh','ftp','dns']
print(service[:2])
service[:2] = ['samba','nfs']
print(service)
测试结果:
['mysql','ssh']
['samba','nfs','ftp','dns']
7.列表的查看
(1)查看出现的次数
service = ['ftp','ssh','ftp','dns']
print(service.count('ftp'))
print(service.count('dns'))
测试结果:
2
1
(2)查看指定元素的索引值(可以指定索引范围查看)
service = ['ftp','ssh','ftp','dns']
print(service.index('ssh'))
print(service.index('ftp',1,4))
测试结果:
1
2
8.列表的排序
(1)sort:按照ASCII 码排序
列表中对字符串的排序是不区分大小写的
service = ['ftp','ssh','ftp','dns']
service.sort()
print(service)
测试结果:
['dns','ftp','ftp','ssh']
(2)random.shuffle:乱序
import random
li = list(range(10))
print(li)
#将原有的列表顺序打乱
random.shuffle(li)
print(li)
测试结果:
<class 'range'>
[0,1,2,3,4,5,6,7,8,9]
[3,1,5,4,9,8,7,2,0,6] ##这一行每一次输出的结果都不同
练习:
1.用户登录
1.系统里面有多个用户,用户的信息目前保存在列表里面
users = [‘root’,‘westos’]
passwd = [‘123’,‘456’]
2.用户登陆(判断用户登陆是否成功
1).判断用户是否存在
2).如果存在
1).判断用户密码是否正确
如果正确,登陆成功,推出循环
如果密码不正确,重新登陆,总共有三次机会登陆
3).如果用户不存在
重新登陆,总共有三次机会
users = ['root','westos']
passwords = ['123','456']
#尝试登录次数
trycount = 0
while trycount < 3:
inuser = input('用户名: ')
inpassword = input('密码: ')
trycount += 1
if inuser in users:
index = users.index(inuser)
password = passwords[index]
if inpassword == password:
print('%s登录成功' %(inuser))
break
else:
print('%s登录失败 : 密码错误' %inuser)
else:
print('用户%s不存在' %inuser)
else:
print('尝试超过三次,请稍后再试')
2.会员信息管理
1.后台管理员用户只有一个 用户:admin 密码:admin
2.管理员登录成功后,才能管理会员信息
3.会员信息包含:
添加会员信息
删除会员信息
查看会员信息
退出
print('管理员登录'.center(50,'*'))
inuser = input('请输入用户名:')
inpasswd = input('请输入密码:')
users = ['westos','root']
passwd = ['123','456']
trycount = 0
for i in range(3):
if inuser == 'admin' and inpasswd == 'admin':
print("""
操作目录:
1.添加会员信息
2.删除会员信息
3.查看会员信息
4.退出
""")
while True:
choice = input('请输入您的选择:')
if (choice == '1') :
add_user = input('用户名:')
add_passwd = input('密码:')
users.append(add_user)
passwd.append(add_passwd)
print('添加成功!')
continue
elif (choice == '2') :
del_user = input('请输入要删除的用户名:')
if del_user in users:
passwd.pop(users.index(del_user))
users.pop(users.index(del_user))
print('%s用户已删除' %del_user)
else:
print("用户不存在")
continue
elif (choice == '3') :
sel_user = input('请输入要查询的用户名:')
if sel_user in users:
print('%s是会员,%s的密码是%s' %(sel_user,sel_user,passwd[users.index(sel_user)]))
else:
print('用户不存在')
continue
elif (choice == '4') :
print('谢谢使用!')
exit()
else:
print('请输入正确的操作!')
continue
else:
print('登录失败,请重试!您还有%d次机会' %(2-i))
i += 1
trycount += 1
continue
else:
print('请稍后重试')