我想按索引号遍历python中的字典。
范例:
dict = {'apple':'red','mango':'green','orange':'orange'}
我想从头到尾遍历字典,以便可以通过字典索引访问字典项。例如,第一项将是苹果,第二项将是芒果,值将是绿色。
像这样:
for i in range(0,len(dict)):
dict.i
我想不出您为什么要这么做的任何理由。如果只需要遍历字典,就可以这样做。
for key, elem in testDict.items():
print key, elem
要么
for i in testDict:
print i, testDict[i]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句