python_字典
程序员文章站
2022-10-04 11:23:46
1.获取字典的所有键 2.获取字典的所有值 3.获取字典的键和值 4.修改和增加元素 5.删除元素 ......
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)
上一篇: goweb-模板引擎
下一篇: ZooKeeper实现生产-消费者队列