print "*************字典操作****************" dictory={"soyo":521,"hadoop":78,"Hbase":100} print dictory["soyo"] dictory["Hbase"]=101 #修改Key的值 print dictory["Hbase"] print dictory dictory["Spark"]=555 #没有key就新增加 print dictory del dictory["hadoop"] #删除指定key的键值对 print dictory print "************字典的遍历*************" dictory2={"soyo1":521,"hadoops":78,"kafka":100} keys=dictory2.keys() print keys keys_s=list(keys) print keys_s keys_s.reverse() print keys_s keys_s.sort() print keys_s print "将字典按照一定规则打印出来,通过改变Key的顺序,然后遍历字典" for i in keys_s: print (i,"=>",dictory2[i])
结果:
************字典的遍历*************
['hadoops', 'soyo1', 'kafka']
['hadoops', 'soyo1', 'kafka']
['kafka', 'soyo1', 'hadoops']
['hadoops', 'kafka', 'soyo1']
将字典按照一定规则打印出来,通过改变Key的顺序,然后遍历字典
('hadoops', '=>', 78)
('kafka', '=>', 100)
('soyo1', '=>', 521)