python字典

#字典:dict     无序,不能使用切片
#列表不能作为k,元组可以作为k
info={"k1":"a","k2":"b","k3":[1,2,"abc"]}       #存放键值对
info1={"k1":"a","k2":"b","k3":[1,2,"abc"],"k4":(1,2),"k5",[[1,2],{"k1":"1","kk2","2"}]}  

v=info['k1']
print(v)                #打印k1
del info['k1']           #删除k1

法1:                  遍历字典所有key
for item in info :    
	print(item)
法2:             遍历字典所有key
for item in info.keys() :
	print(item)

for item in info.values() :            遍历字典所有values
	print(item)


方法1:                      遍历字典所有key,value
for item in info.keys() :            遍历字典所有key,value
	print(item,info[item])
方法2:
for k,v in info.items():
	print(k,v)

v=dict.fromkeys([“k1”,123,”999”],123)  #把[“k1”,123,”999”]当成键,生成字典

info={"k1":"a","k2":"b

你可能感兴趣的:(python常用知识,各类程序脚本,中间件部署,python,开发语言)