标签:coding items 删除 def clear use bin 返回值 返回
#!/user/bin/python
# -*- coding:utf-8 -*-
dic1 = {‘age‘:24,‘name‘:‘zd‘,‘sex‘:‘boy‘}
print(dic1)
#增
dic1[‘high‘] = 175 #没有建就添加
dic1[‘sex‘] = ‘boy1‘ #有建就修改
print(dic1)
dic1.setdefault(‘weight‘,232) #有的话不做任何操作m没有的话就添加
print(dic1)
#删
print(dic1.pop(‘age‘))#有返回值按键去删除
print(dic1.pop(‘czxcxz‘,None))#可设置返回值
print(dic1)
dic1.popitem()#默认从最后删
print(dic1)
print(dic1.popitem())
del dic1[‘name‘]
print(dic1)
dic1.clear() #清空
print(dic1)
#查
dic1 = {‘age‘:24,‘name‘:‘zd‘,‘sex‘:‘boy‘}
print(dic1.keys())
print(dic1.values())
print(dic1.items())
for i in dic1:
print(i)
for k,v in dic1.items():
print(k,v)
2017-12-20python全栈9期第五天第二节之字典的增删查改和字典的for循环
标签:coding items 删除 def clear use bin 返回值 返回
原文地址:https://www.cnblogs.com/zhang-da/p/10205046.html