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
  • 相关阅读:
    js高级1
    JUC总览
    7 种阻塞队列相关整理
    Exchanger 相关整理
    Semaphore 相关整理
    ConcurrentLinkedQueue 相关整理
    ConcurrentHashMap(1.8) 相关整理
    ConcurrentHashMap(1.7) 相关整理
    ReentrantReadWriteLock 相关整理
    LockSupport 工具相关整理
  • 原文地址:https://www.cnblogs.com/AngueTone/p/7340748.html
Copyright © 2011-2022 走看看