【问题标题】:dictionary in python? [closed]python中的字典? [关闭]
【发布时间】:2010-11-03 21:23:47
【问题描述】:

我在python中以多少种方式遍历字典???

【问题讨论】:

标签: python dictionary traversal


【解决方案1】:

如果我对您的问题的解释正确,您可以通过多种方式横穿字典。

适合初学者的好书位于here

此外,字典可能不是您最好的选择。更多信息会很有帮助,更不用说它会帮助您。

【讨论】:

    【解决方案2】:

    多种方式!

    testdict = {"bob" : 0, "joe": 20, "kate" : 73, "sue" : 40}
    
    for items in testdict.items():
        print (items)
    
    for key in testdict.keys():
        print (key, testdict[key])
    
    for item in testdict.iteritems():
        print item
    
    for key in testdict.iterkeys():
        print (key, testdict[key])
    

    仅此而已,但它开始从这些简单的方式转向更复杂的方式。所有代码都经过测试。

    【讨论】:

      【解决方案3】:

      http://docs.python.org/tutorial/datastructures.html#looping-techniques

      >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
      >>> for k, v in knights.iteritems():
      ...     print k, v
      ...
      gallahad the pure
      robin the brave
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多