python之列表
1. 列表的定义
# 列表里可以存储不同的数据类型
lie = [1,3,'a','g',True] # 定义列表,有不同类型的数据
print(lie) # 输出列表
print(type(lie)) # 查看列表内容# 列表里也可以嵌套列表
lie1 = [1,2,3,True,[1,2,3]]
print(lie1)
print(type(lie1))
2.列表的特性
s=['http','ftp','nginx']
# 索引
print(s[0])print(s[-1])
# 切片
print(s[1:])
print(s[::-1])print(s[:-1])
# 重复
print(s[:]*2)
# 连接
s1=['ssh','mysql',]
print(s+s1)
# 成员操作符
print('http' in s)
print('iptables' in s)
# for循环遍历
for i in s:
print(i)
# 列表里嵌套列表
s2 = [['http',80],['ssh',22]]
# 索引
print(s2[0][1])
print(s2[-1][1])
# 切片
print(s2[:][1]) # 输出s2中嵌套的所有列表的第二个字符串
print(s2[:-1][0]) # 输出s2中嵌套的倒数第一个列表的第一个字符串
3.列表的应用-判断季节
month=int(input('请输入月份:'))
if month in [3,4,5]:
print('%d月是春季' %month)
elif month in [6,7,8]:
print('%d月是夏季' %month)
elif month in [9,10,11]:
print('%d月是秋季' %month)
elif month in [12,1,2]:
print('%d月是冬季' %month)
else:
print('请输入正确的月份')
4.列表的应用-连接列表
假定有下面的列表:
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])
5.列表的增删改查
(1)添加
service = ['http','ssh','ftp']
# append:追加一个元素到列表中
service.append('nginx')
print(service)
# extend:拉伸,追加多个元素到列表中
service.extend(['firewalld','mysql'])
print(service)
# insert:在指定位置插入元素
service.insert(1,'samba')
service.insert(2,'iptables')
print(service)
(2)删除
service = ['http','ssh','nginx']
# remove:删除指定元素,并且查看不到内容
a=service.remove('ssh')
print(service)
print(a) # 查看不到a
# pop:弹出元素,默认先弹出最后一个元素,依次往前,但是可以查看到被弹走的内容
b=service.pop()
print(service)
print(b) # 可以查看到b
# del关键字,从内存中删除
print(service)
del serviceprint(service)
(3)列表的修改
service = ['http','ssh','nginx']
# 通过索引,重新赋值
service[0] = 'mysql'print(service)
# 通过切片
print(service[:2])
service[:2] = ['samba','firewalld'] # 把列表中下标2之前的元素都修改
print(service)
(4)列表的查看
service=['http','ftp','nginx','http']
# 查看出现的次数
print('ftp出现的次数为:',service.count('ftp'))
# 查看指定元素的索引值(可以指定索引范围来查看)
print('nginx的索引值为:',service.index('nginx'))
print('在索引值为0~3之间查找到的http的索引值为:',service.index('http',0,3))
print('在索引值为1~4之间查找到的http的索引值为:',service.index('http',1,4))
注:如果在范围内有多个索引值,返回的是小的索引值
(5)列表的排序
import random
service = ['Http','Ftp','ssh','mysql','ftp']
service.sort() # 按照ASC码顺序排列
print(service) # 输出排列后的列表
service.sort(key=str.lower) # 按照小写字母顺序排列
print(service) # 输出排列后的列表
service.sort(key=str.upper) # 按照大写字母顺序排列
print(service) # 输出排列后的列表
random.shuffle(service) # 把列表原有的顺序打乱
print(service) # 输出打乱后的列表
注:字符串排序不区分大小写,所以按照大写字母排序和按照小写字母排序是一样的
6.综合应用
练习1:用户登录系统
users = ['root','westos']
passwds = ['123','456']
print('用户管理系统'.center(30,'*'))
for i in range(3):
user = input('请输入用户名:')
passwd = input('请输入用户密码:')
if user in users:
if passwd in passwds:
print('登录成功')
break
else:
i+=1
print('密码错误,你还有%d次机会' %(3-i))
else:
i+=1
print('用户不存在,你还有%d次机会' %(3-i))
else:
print('登录超过三次,请稍后登录')
练习2:用户管理系统
users = ['root','westos']
passwds = ['123','456']
print('用户管理系统'.center(50,'*'))
for i in range(3):
user = input('请输入用户名:')
passwd = input('请输入密码:')
if user in users:
if passwd in passwds:
print('登录成功\t')
while True:
print('''操作目录:\n',
1.添加用户
2.删除用户
3.查看用户
q.退出
''' )
a=input('请输入您的操作:')
if a == '1':
print('添加用户'.center(50, '*'))
Adduser = input('用户名:')
if Adduser in users:
print('%s用户已经存在' %Adduser)
else:
users.append(Adduser)
Addpasswd = input('密码:')
passwds.append(Addpasswd)
print('添加%s成功' %Adduser)
elif a == '2':
print('删除用户'.center(50,'*'))
Deluser = input('用户名:')
Delindex = users.index(Deluser) # 获取Deluser的索引
users.remove(Deluser)
passwds.pop(Delindex)
print('删除%s用户成功' %Deluser)
elif a == '3':
print('查看用户信息'.center(50,'*'))
print('\t用户名\t密码')
usercount = len(users)
for i in range(len(users)):
print('\t%s\t%s' %(users[i],passwds[i]))
elif a == 'q':
exit()
else:
print('请输入正确的选择')
else:
i+=1
print('密码错误,请重新登录,您还有%d次机会' %(3-i))
else:
i+=1
print('用户不存在请重新登录,您还有%d次机会' %(3-i))
else:
print('登录超过三次,请稍后登录')
练习3:列表实现栈的工作原理
stack = [] # 定义空栈
print("""栈操作
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空
q.退出
""")
while True:
choice = input('请输入你的选择:')
if choice == '1':
instack = input('入栈元素为:')
stack.append(instack)
print('%s入栈成功' %instack)
elif choice == '2':
# 先判断栈是否为空
if not stack:
print('栈为空,不能出栈')
else:
outstack = stack.pop()
print('%s元素出栈成功' %outstack)
elif choice == '3':
if len(stack) == 0:
print('栈为空')
else:
print('栈顶元素为: %s' %(stack[-1]))
elif choice == '4':
print('栈的长度为:',len(stack))
elif choice == '5':
if not stack:
print('栈为空')
else:
print('栈不为空')
else:
print('请输入正确的选择')