In [26]: test = {}
In [27]: test["apple"] = "green"
In [28]: test["banana"] = "yellow"
In [29]: test["orange"] = "orange"
In [32]: for fruit, colour in test:
....: print fruit
....:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home1/users/joe.borg/<ipython-input-32-8930fa4ae2ac> in <module>()
----> 1 for fruit, colour in test:
2 print fruit
3
ValueError: too many values to unpack
我想要的是迭代測試並將鍵和值合在一起。如果我只是做一個for item in test:
我只能得到鑰匙。Python遍歷字典
的最終目標的一個例子是:
for fruit, colour in test:
print "The fruit %s is the colour %s" % (fruit, colour)
看到'幫助(字典)' – u0b34a0f6ae 2011-12-21 12:29:32
爲什麼不'在測試的水果:打印「果實%s是顏色% s「%(水果,測試[水果])'? – mtrw 2011-12-21 12:32:28