一.创建列表
数组:存储同一数据类型的集合
列表:可以存储任意数据类型的集合
#列表里可以存储不同的数据类型
list1 = [1,1.2,'hello',True]
print(list1)
print(type(list1))
#列表嵌套
li1 = [1,1.2,'hello',True,[1,2,3,4,5]]
print(list1)
print(type(list1))
二.列表的特性
service = [ 'ftp','http','dns','apache']
#索引
print(service[1])
print(service[0])
print(service[-1])
#切片
print(service[1:])
print(service[:-1])
print(service[::-1])
#重复
print(service * 3)
#连接
service2 = ['mysql','firewalld']
print(service+service2)
#成员操作符
print('mysql' in service)
print('mysql' in service2)
#迭代
print('显示所有的服务:'.center(50,'*'))
for ser in service:
print(ser)
#列表里嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
#索引
print(service2[1][1])
print(service2[-1][0])
#切片
print(service2[:-1][:])
print(service2[:][1])
print(service2[:-1][1])
三.列表的增加
service = ['http','ftp','dns']
#1.
print(service+['myqsl'])
#2.append 追加 追加一个元素到列表中
service.append('mysql')
print(service)
#3.expend 拉伸 追加多个元素到列表中
service.extend(['mysql','apache'])
print(service)
#4.insert 在指定索引位置插入元素
service.insert(0,'samba')
print(service)
四.列表的删除
#1.pop 弹出指定元素,默认弹出列表中最后一个元素
service = ['samba','ftp','http']
print(service.pop(0))
print(service)
#2.remove 删除指定元素
ser=service.remove('ftp')
print(service)
print(ser)
#3.del关键字 从内存中删除
print(service)
del service
print(service)
五.列表的修改
service = ['samba','ftp','http','dns']
#1.通过索引,直接赋值
service[2]='mysql'
print(service)
#2.通过切片,替换插入位置后的元素
service[0:] = ['mysql','firewalld']
print(service)
六.列表的查看
service = ['http','dns','dns','samba']
#查看出现的次数
print(service.count('dns'))
print(service.count('samba'))
##查看指定元素的索引值(可以指定索引范围查看)
print(service.index('dns'))
print(service.index('dns',2,3))
七.列表的排序
#列表中元素是字符串排序,是看列表中元素的首字母对应的ASCII值
service = ['http','dns','Dns','samba']
service1 = sorted(service)
print(service1)
#打乱原有的列表顺序
import random
num=list(range(10))
print(num)
random.shuffle(num)
print(num)
八.列表的小练习
列表的练习1:
"""
假定有下面的列表:
names = ['orange','pear','peach','apple']
输出结果为: 'I have orange, pear, peach and apple.'
"""
names = ['orange','pear','peach','apple']
print('I have '+','.join(names[:-1])+' and '+names[-1])
列表的练习2:用户管理
"""
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('尝试超过三次,请稍后再试')
列表的练习3:会员信息管理
print('管理员登陆'.center(50,'*'))
inuser = input('Username:')
inpasswd = input('Password:')
users = ['root','westos']
passwords =['123','456']
if inuser == 'admin' and inpasswd == 'admin':
print('欢迎登陆会员管理系统'.center(30,'*'))
print("""
操作目录
1.添加会员信息
2.删除会员信息
3.查看会员信息
4.退出
""")
while 1:
choice = input('请输入您的选择:')
if choice == '1':
user=input('会员用户:')
passwd=input('会员密码:')
if (user in users) == True:
# print('%s用户已存在' %user)
index=users.index(user)
passwords[index] = passwd
else:
users.append(user)
passwords.append(passwd)
print('')
elif choice == '2':
user=input('请你选择删除会员用户名:')
if (user in users) == True:
index = users.index(user)
users.pop(index)
passwords.pop(index)
print('删除会员用户成功!')
print('')
else:
print('你删除的会员用户不存在')
print('')
elif choice == '3':
print(users)
print(passwords)
elif choice == '4':
exit()
else:
print('输入错误,请重新输入')
print('')
else:
print('登陆失败!')
列表的练习4:栈的工作原理
"""
栈的工作原理
入栈
出栈
栈顶元素
栈的长度
栈是否为空
"""
stack = []
print("""
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空
6.退出
""")
while(1):
choice = int(input('请输入您的选择:'))
#1.入栈
if choice == 1:
name=input('请输入入栈的元素:')
stack.append(name)
print('%s入栈成功' %name)
#2.出栈
elif choice == 2:
if len(stack) != 0:
sta=stack.pop()
print('%s 出栈成功' %sta)
else:
print('栈为空,无法出栈')
#3.栈顶元素
elif choice == 3:
if len(stack) != 0:
print('栈顶元素:%s' %(stack[-1]))
else:
print('栈为空,无栈顶元素!')
#4.栈长度
elif choice == 4:
print('栈的长度为%d' %(len(stack)))
#5.栈是否为空
elif choice == 5:
if len(stack) != 0:
print('栈不为空')
else:
print('栈为空')
#6.退出
else:
exit()
这篇博客详细介绍了Python中的列表,从创建到各种操作,包括增加、删除、修改和查看元素,以及排序和实战小练习,如用户管理和栈的工作原理。
5059

被折叠的 条评论
为什么被折叠?



