python -dict
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python -dict相关的知识,希望对你有一定的参考价值。
#字典的添加、删除、修改操作
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
dict["w"] = "watermelon" #添加
del(dict["a"]) #删除
dict["g"] = "grapefruit"
print dict.pop("b")
print dictdict.clear() #清空
print dict
#字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for k in dict:
print "dict[%s] =" % k,dict[k]
#字典items()的使用
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
每个元素是一个key和value组成的元组,以列表的方式输出print dict.items()
#调用items()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for (k, v) in dict.items():
print "dict[%s] =" % k, v
#调用iteritems()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print dict.iteritems()
for k, v in dict.iteritems():
print "dict[%s] =" % k, v
for (k, v) in zip(dict.iterkeys(), dict.itervalues()):
print "dict[%s] =" % k, v
以上是关于python -dict的主要内容,如果未能解决你的问题,请参考以下文章