您好,以下是我在jupyter笔记本中执行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)
答案 0 :(得分:0)
缩进错误,意味着在某处使用的制表符(或空格)太少或太多,现在代码块位于最右边或最左边,以至于无法正确解释。
我发现的那些:
-您定义remove_common_text()的整个块应向左移动一个标签(第1-27行)
-第2行的第一个if len(lst) > 4:
之前有一个缩进,因此该函数定义中的所有后续行
-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)