练习描述
1.员工管理系统
字典或者数据的嵌套使用完成更加完善的员工管理
2.去重下列列表并保留数据值原来的顺序
eg: [1,2,3,2,1] 去重之后 [1,2,3]
l1 = [2,3,2,1,2,3,2,3,4,3,4,3,2,3,5,6,5]
3.有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={'jason','oscar','kevin','ricky','gangdan','biubiu'}
linuxs={'kermit','tony','gangdan'}
1. 求出即报名python又报名linux课程的学员名字集合
2. 求出所有报名的学生名字集合
3. 求出只报名python课程的学员名字
4. 求出没有同时这两门课程的学员名字集合
4.统计列表中每个数据值出现的次数并组织成字典战士
eg: l1 = ['jason','jason','kevin','oscar']
结果:{'jason':2,'kevin':1,'oscar':1}
真实数据
l1 = ['jason','jason','kevin','oscar','kevin','tony','kevin']
题解
# 练习1
"""用户系统 """
user_number = 1 # 用户数
user_massage = {
0: {'name': 'jason', 'password': '123', 'age': '20'}} # 用户信息字典
while True:
user_choose = input('''
————————————————————
输入1执行添加用户名功能
输入2执行查看所有用户名功能
输入3执行删除指定用户名功能
————————————————————-
请输入>>>:\
''').strip()
if user_choose == '1':
name = input('请输入姓名:').strip()
password = input('定义一个密码:').strip()
age = input('请输入您的年龄:').strip()
one_user_massage = {}
one_user_massage['name'] = name
one_user_massage['password'] = password
one_user_massage['age'] = age
user_massage[user_number] = one_user_massage
print(user_massage)
user_number += 1
print(f'当前有{user_number}个用户')
elif user_choose == '2':
print('姓名 密码 年龄')
for i in user_massage:
temporary_dict = user_massage[i]
print(f"{temporary_dict['name']} {temporary_dict['password']} {temporary_dict['age']}")
elif user_choose == '3':
for i in user_massage:
temporary_dict = user_massage[i]
print(f"{temporary_dict['name']}", end=' ')
print()
del_name = input('请输入你要删除的用户名字>>:').strip()
for i in list(user_massage): # 在字典遍历的时候 不能修改字典元素 RuntimeError: dictionary changed size during iteration
if del_name == user_massage[i]['name']:
del user_massage[i]
print('已删除=。=')
# 练习2
old_list = [2, 3, 2, 1, 2, 3, 2, 3, 4, 3, 4, 3, 2, 3, 5, 6, 5]
new_list = []
for num in old_list:
if num not in new_list:
new_list.append(num)
print(new_list)
# 练习3
pythons = {'jason', 'oscar', 'kevin', 'ricky', 'gangdan', 'biubiu'}
linuxs = {'kermit', 'tony', 'gangdan'}
print(pythons & linuxs)
print(pythons | linuxs)
print(pythons - linuxs)
print(pythons ^ linuxs)
# 练习4
mixed_list = ['jason', 'jason', 'kevin', 'oscar', 'kevin', 'tony', 'kevin']
clear_list = [] # 装有不重复的用户名
times = 0 # 对单个用户的计数
user_dict = {} # 结果的字典
for user in mixed_list:
if user not in clear_list:
clear_list.append(user)
for user in clear_list:
for mix_user in mixed_list:
if user == mix_user:
times += 1
user_dict[user] = times
times = 0
print(user_dict)