IndentationError:尝试为文档复制LDA时应为缩进块

2024-04-20 12:58:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我收藏的第一篇文章中获得LDA分布,但我遇到了几个错误:

我的收藏:doc_set,是一个pandas.core.series.Series。每当我想运行简单的代码时:

print(ldamodel[doc_set[1]])

我运行了以下错误:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().,我想我解决了这个问题:

if doc_set is not None:
print(ldamodel[doc_set[1]])

然而,现在我得到了以下错误:IndentationError: expected an indented block。我在寻找错误的直觉,而不是纠正,我不能把我的整个LDA复制,因为它太大了。提前谢谢!你知道吗


Tags: the代码corepandasdocis错误文章
2条回答

缩进不正确,因为没有将print语句放在if块中。如果以冒号(:)结束行,则必须增加缩进级别,否则将出现IndentationError异常。
这将是正确的代码:

if doc_set is not None:
    print(ldamodel[doc_set[1]])

缩进在python中非常特殊。您必须通过为每个块使用空格或制表符来保持继承权。每个块只能有制表符或(任意数量的)空格。你知道吗

for item in list:
    print item

if flag:
  raise SystemExit

在第一个代码块中,我使用了四个空格,在第二个代码块中,我使用了两个空格。你知道吗

评论也是如此。注释必须相应缩进。你知道吗

print 'Starting module'
if not configs:
    '''
    sys.exit('Error in Configuration files.')
    '''
    pass

在本例中,行是要注释的,python不会抱怨。否则它将抛出有关缩进的错误。你知道吗

相关问题 更多 >