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)

 

posted @ 2019-08-21 18:19  手可摘星辰。  阅读(220)  评论(0编辑  收藏  举报