python - Jupyter 笔记本中的错误“IndentationError:预期有缩进块”
问题描述
嗨,下面是我在 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 行的第一个之前有一个缩进,因此在第 2 行中的所有以下行函数定义 - 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)
推荐阅读
- docker - 无法从谷歌云构建访问 pip 主机
- android - Flutter App 构建发布问题(Firestore)
- sapply - 在 sapply 中访问向量的先前值
- ios - 应用因隐私而被应用商店拒绝 - 数据收集和存储
- java - 如何在java中使用密钥接口作为密钥?
- assembly - SPARC v8:本地寄存器使用
- python - 如何使用套接字编程将字典数据从客户端发送到 python 服务器以与 df2 进行比较?
- go - Golang 中的 Google Pay PaymentMethodTokenRecipient 函数实现
- sql - 选择每组中除最大记录外的所有记录
- testing - grabAttributeFrom() 方法不返回属性值