字典
设置空字典
#定义字典
1.使用{}
dict1 = {} # 空的字典
dict2 = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
__________________________________
2.利用 dict函数创建字典
dict3 = dict(name='王峰', sex='男', hiredate='1997-10-20')
print(dict3)
**结果**
{'name': '王峰', 'sex': '男', 'hiredate': '1997-10-20'}
________________________________
formakeys()
以列表载体形式,向字典内传值
dict={}
dict4 = dict.fromkeys(['name', 'sex' , 'hiredate', 'grade'],"N/A")
print(dict4)
**结果**
{'name': 'N/A', 'sex': 'N/A', 'hiredate': 'N/A', 'grade': 'N/A'}
字典取值操作
字典名[key] 形式取值
employee = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
#字典取值
name = employee['name']
print(name)
**结果**
王峰
.get(key)取值
employee = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
print(employee.get("job"))
**结果**:销售
__________________________________________
#取值如果不存在,直接添加参数并且不会报错
print(employee.get('dept', '其他部门'))
#虽然没有有dept这个key,但是此方法不存在key时可以添加在字典中添加参数
**结果**:其他部门
del 字典名[key]清除字典键值对
del employee['name']
in 成员运算符
判断是否在不在字典中
employee = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
#判断是否不在字典中
print('name' not in employee) #False
print('dept' not in employee) #True
遍历字典
employee = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
#默认遍历字典key
for key in employee:
v = employee[key]
print(v)
________________________
#### .items()遍历字典键值对
for k,v in employee.items():
print(k,v)
字典更新操作
employee[键名] = 值
update()对多个kv进行更新
employee = {'name': '王峰', 'sex': "男",
'hiredate': '1997-10-20', 'grade': 'A',
'job': '销售', 'salary': 1000,
'welfare': 100
}
employee.update(weight=80,dept='财务部')
print(employee)
**结果**
{'name': '王峰', 'sex': '男', 'hiredate': '1997-10-20', 'grade': 'B', 'job': '销售', 'salary': 1200, 'welfare': 150, 'dept': '财务部', 'weight': 80}
clear()清空字典
employee.clear()
setdefault()
为字典设置默认值,如果某个key存在则忽略
类似:
emp1=dict(name = "jacky", grade="B")
emp2 = {'name':'Lily' , 'grade' : 'A'}
#if "grade" not in emp2:
#emp2['grade'] = 'C'
setdefault()
emp1.setdefault('grade', 'C')
emp2.setdefault('grade', 'C')
print(emp2)
**结果**
{'name': 'Lily', 'grade': 'A'} #因为键已经存在所以结果不变
获取字典视图*
为什么说是获取字典视图呢,因为获取的视图dict_keys,dict_values,dict_items,都是根据字典的改变而改变
keys/values/items
1keys代表获取所有的键
emp1 = {'name':'Jacky' , 'grade' : 'B'}
ks =emp1.keys()
print(ks)
**结果**
dict_keys(['name', 'grade']) #为什么不打印出正常的列表呢:因为这是字典视图会根据字典值得改变而改变
2values 代表获取所有值
vs = emp1.values()
print(vs)
print(type(vs))
**结果**
dict_values(['Jacky', 'B'])
3items 代表获取所有的键值对
its = emp1.items()
print(its)
print(type(its))
**结果**
dict_items([('name', 'Jacky'), ('grade', 'B')])
# 3. format_map()利用字典格式化字符串
老版本的字符串格式化 %分形式
emp_str = "姓名:%(name)s,评级:%(grade)s,入职时间:%(hiredate)s" %emp1
print(emp_str)
新版本的字符串格式化 format_map()形式
emp_str1 = "姓名:{name},评级:{grade},入职时间:{hiredate}".format_map(emp1)
print(emp_str1)
字典底层,哈希
### 实战例子员工查询
source = "7782,CLARK,MANAGER,SALES,5000$7934,MILLER,SALESMAN,SALES,3000$7369,SMITH,ANALYST,RESEARCH,2000"
employee_list = source.split("$")
print(employee_list)
**结果**
['7782,CLARK,MANAGER,SALES,5000', '7934,MILLER,SALESMAN,SALES,3000', '7369,SMITH,ANALYST,RESEARCH,2000']
#保存所有解析后的员工信息,key是员工编号,value则是包含完整员工信息的字典
all_emp = {}
for i in range(0,len(employee_list)):
#print(i)
e = employee_list[i].split(',')
print(e)
#创建员工字典
employee = dict(no=e[0],name=e[1],job=e[2],department=e[3],salary=e[4])
# print(employee)
all_emp[employee["no"]]=employee
print(all_emp)
**结果**
{'7782': {'no': '7782', 'name': 'CLARK', 'job': 'MANAGER', 'department': 'SALES', 'salary': '5000'}, '7934': {'no': '7934', 'name': 'MILLER', 'job': 'SALESMAN', 'department': 'SALES', 'salary': '3000'}, '7369': {'no': '7369', 'name': 'SMITH', 'job': 'ANALYST', 'department': 'RESEARCH', 'salary': '2000'}}
empno = input("请输入员工编号:")
emp = all_emp.get(empno)