首页 > 技术文章 > python的字典dict 遍历和赋值操作

aitree 2021-01-27 15:08 原文

import pdb
if __name__ == "__main__":
    #创建字典
    dic = {"url1":"http://www.qq.com","url2":"http://id.qq.com"}
    #更新字典
    dic["url3"] = "http://qzone.qq.com"
    #遍历字典
    for key in dic.keys():
        if key == "url1":
            #更新字典
            dic[key] = "http://www.qq.com/wenle"

    #遍历字典
    for key,value in dic.iteritems():
        print "key:[%s],value:[%s]"%(key,value)

    #遍历字典
    for key in dic.iterkeys():
        print "key:[%s]"%(key)

    #遍历字典
    for value in dic.itervalues():
        print "value:[%s]"%(value)

 

推荐阅读