我以多少种方式遍历python中的字典???
nazmul hasan
问问题
1606 次
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 回答