2024-05-29 01:42:52 发布
网友
参考Listing 9. Iteration and a dictionary
>>> d = {0: 'zero', 3: 'a tuple', 'two': [0, 1, 2], 'one': 1} >>> for k in d.iterkeys(): ... print(d[k]) File "<stdin>", line 2 print(d[k]) ^ IndentationError: expected an indented block
为什么?
Python 3没有iterkeys。只需使用:
iterkeys
for k in d: print(d[k])
或者更好:
for v in d.values(): print(v)
即使在使用Python交互式解释器时,也需要确保对新的代码块进行了一些缩进。
这:
>>> for k in d.iterkeys(): ... print(d[k])
应该是这样的:
顺便说一下:该链接在预期输出中有很多错误,可能是一些复制/粘贴问题?
Indentation level of your statements is significant in Python.
Python 3没有
iterkeys
。只需使用:或者更好:
即使在使用Python交互式解释器时,也需要确保对新的代码块进行了一些缩进。
这:
应该是这样的:
顺便说一下:该链接在预期输出中有很多错误,可能是一些复制/粘贴问题?
Indentation level of your statements is significant in Python.
相关问题 更多 >
编程相关推荐