zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    局域网组网总目录
    VLAN之间的通信
    DHCP
    ACL
    linux 程序后台运行
    VLAN
    VTP
    dubbox生产者与消费者案例
    String data jpa执行的增删改查
    StringBoot整合Mytais实现数据查询与分页
  • 原文地址:https://www.cnblogs.com/AngueTone/p/7340748.html
Copyright © 2011-2022 走看看