我有一个 dict 喜欢:
test = {0: {'nom': 'toto', 'id':1}, 2: {'nom': 'tutu', 'id': 2}}
我想打印一个结果
toto
tutu
我试试
for nom in test :
print test['nom']
但不工作
谢谢
我有一个 dict 喜欢:
test = {0: {'nom': 'toto', 'id':1}, 2: {'nom': 'tutu', 'id': 2}}
我想打印一个结果
toto
tutu
我试试
for nom in test :
print test['nom']
但不工作
谢谢
因为你的第一个字典的键是数字。不是“名义”。
for k in test:
print test[k]['nom']
test = {0: {'nom': 'toto', 'id':1}, 2: {'nom': 'tutu', 'id': 2}}
print ' '.join([test[ele]['nom'] for ele in test])
for nom in test:
print (test[nom]['nom'])
这应该适用于 Python 3 和 2。使用以下内容仅适用于 2
for nom in test:
print test[nom]['nom']