-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 に答える