我是 Python 的新手。我正在尝试在 python 中迭代一个列表并以这种方式打印元素:
for i in list
print i
Run Code Online (Sandbox Code Playgroud)
但我收到这样的错误:
for i in list
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我有一个类似的代码,它工作正常:
list_unique = []
for i in list:
if i not in list_unique:
list_unique.append(i)
Run Code Online (Sandbox Code Playgroud)
我无法理解其中的区别。有人可以帮忙吗?
你缺少一个“:”
for i in list: #This one!
print i
Run Code Online (Sandbox Code Playgroud)
在 Python 中,您必须以“:”开始新块的 for、if、while、def、class 和其他语句结束。