繁体   English   中英

Jupyter 笔记本中的错误“IndentationError:预期有缩进块”

[英]Error in Jupyter notebook “IndentationError: expected an indented block”

嗨,下面是我在 jupyter notebook 中用于执行 if 条件的代码。 但是会出现缩进块的错误。 我猜这是因为空间问题。 但是我不确定如何解决它。 你能帮我解决这个问题吗?

    def remove_common_text(lst):
            if len(lst) > 4:
                for itm in lst:
                    if len(itm)>1:
                        rep = True
                        while rep == True:
                            ctr = 0
                            ctr2 = 0
                            for itm2 in lst:
                                if len(itm2)>0:
                                    if itm[0] == itm2[0]:
                                        ctr = ctr + 1
                                    if itm[-1] == itm2[-1]:
                                        ctr2 = ctr2 + 1
                           if ctr > 4:
                                str = itm[0]
                                for itm2 in lst:
                                    if len(itm2)>0:
                                        if itm2[0] == str and len(itm2)>1:
                                            del itm2[0]
                            if ctr2 > 4:
                                str2 = itm[-1]
                                for itm2 in lst:
                                    if len(itm2)>0:
                                        if itm2[-1] == str2 and len(itm2)>1:
                                            del itm2[-1]
                            if (ctr <= 4 and ctr2 <= 4) or len(itm)<=1: rep = False
    id_unique = documents['ID'].unique()
    print(id_unique)
for id in id_unique:


remove_common_text(documents[documents.ID==id]['split_clean'])
print(id)

缩进错误,意味着在某处使用了太少或太多的制表符(或空格),现在代码块被定位在最右边或最左边以被正确解释。 我发现的那些: - 定义 remove_common_text() 的整个块应该向左移动一个选项卡(第 1-27 行) - if len(lst) > 4:在第一个之前有一个缩进2 因此,function 定义中的所有以下行 - if ctr > 4:第 15 行)缺少一个空格以正确缩进

如果你修复了这些,你会得到一个没有缩进错误的代码:

def remove_common_text(lst):
    if len(lst) > 4:
        for itm in lst:
            if len(itm)>1:
                rep = True
                while rep == True:
                    ctr = 0
                    ctr2 = 0
                    for itm2 in lst:
                        if len(itm2)>0:
                            if itm[0] == itm2[0]:
                                ctr = ctr + 1
                            if itm[-1] == itm2[-1]:
                                ctr2 = ctr2 + 1
                    if ctr > 4:
                        str = itm[0]
                        for itm2 in lst:
                            if len(itm2)>0:
                                if itm2[0] == str and len(itm2)>1:
                                    del itm2[0]
                    if ctr2 > 4:
                        str2 = itm[-1]
                        for itm2 in lst:
                            if len(itm2)>0:
                                if itm2[-1] == str2 and len(itm2)>1:
                                    del itm2[-1]
                    if (ctr <= 4 and ctr2 <= 4) or len(itm)<=1: rep = False
id_unique = documents['ID'].unique()
print(id_unique)
for id in id_unique:
    remove_common_text(documents[documents.ID==id]['split_clean'])
print(id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM