-4

我有一个 dict 喜欢:

test =  {0: {'nom': 'toto', 'id':1}, 2: {'nom': 'tutu', 'id': 2}}

我想打印一个结果

toto

tutu

我试试

for nom in test :
   print test['nom']

但不工作

谢谢

4

3 回答 3

1

因为你的第一个字典的键是数字。不是“名义”。

for k in test:
    print test[k]['nom']
于 2013-10-25T16:44:51.420 回答
0
 test = {0: {'nom': 'toto', 'id':1}, 2: {'nom': 'tutu', 'id': 2}}

 print ' '.join([test[ele]['nom'] for ele in test])
于 2013-10-25T16:40:18.723 回答
0
for nom in test:
       print (test[nom]['nom'])

这应该适用于 Python 3 和 2。使用以下内容仅适用于 2

for nom in test:
           print test[nom]['nom']
于 2013-10-25T16:51:57.433 回答