-1

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

4

3 回答 3

1

很多种方法!

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])

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

于 2009-06-18T04:45:50.653 回答
0

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

适合初学者的好读物位于此处

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

于 2009-06-18T04:44:59.000 回答
0

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
于 2009-06-18T04:46:57.143 回答