python_字典
1.获取字典的所有键
info = {'name':'wdc','age':18,'gender':'男'} # 打印字典的所有键 for i in info.keys(): print(i)
2.获取字典的所有值
info = {'name':'wdc','age':18,'gender':'男'} # 打印字典的所有值 for i in info.values(): print(i)
3.获取字典的键和值
info = {'name':'wdc','age':18,'gender':'男'} # 将键和值分别赋给i和j for i,j in info.items(): print(i,j)
info = {'name':'wdc','age':18,'gender':'男'}
# 将字典的每个键值对生成一个元组
for i in info.items():
print(i)
4.修改和增加元素
info = {'name':'wdc','age':18,'gender':'男'} # 如果字典中已经有了xvehao键则修改,否则酒增加 info['xvehao'] = 123 print(info)
5.删除元素
info = {'name':'wdc','age':18,'gender':'男'} # 删除键为gender的键值对 del info['gender'] print(info)
6.删除元素并获取其值
info = {'name':'wdc','age':18,'gender':'男'} # 删除键为'name'的键值对,并返回其值 qqq = info.pop('name') print(info) print(qqq)