for n in d:
print n+':',d[n]
1 回答

pardon110
TA贡献1038条经验 获得超227个赞
如果你迭代的对象是字典类型,这样写没问题,但如果d是元组或列表(就n代表值而非键),很容易出现索引越界的错误。其原因在于python 中的for ... in结构会视不同的迭代对象采用不同的迭代策略。这就好比golang中的for...range结构,在对字符串与通道类型呈现不同的结果。
添加回答
举报
0/150
提交
取消