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
Quello che voglio è iterare sul test e ottenere la chiave e il valore insieme. Se faccio solo un for item in test:
, ottengo solo la chiave.Python iterate su un dizionario
Un esempio di obiettivo finale sarebbe:
for fruit, colour in test:
print "The fruit %s is the colour %s" % (fruit, colour)
veda 'help (dict)' – u0b34a0f6ae
Perché non 'per la frutta in prova: print "Il frutto% s è il colore% s "% (frutta, prova [frutto])'? – mtrw