python - 使用多个 if 条件和 next() 时的 StopIteration 错误/异常/错误
问题描述
我在这里使用了 jupyter 笔记本。
此代码来自 youtube 视频。它在 youtuber 的计算机上工作,但我的提出了 Stopiteration 错误
在这里,我试图获取与“Go”语言相关的所有标题(来自 csv 的问题)
import pandas as pd
df = pd.read_csv("Questions.csv", encoding = "ISO-8859-1", usecols = ["Title", "Id"])
titles = [_ for _ in df.loc[lambda d: d['Title'].str.lower().str.contains(" go "," golang ")]['Title']]
#新单元格
import spacy
nlp = spacy.load("en_core_web_sm" , disable= ["ner"])
#新单元格
def has_golang(text):
doc = nlp(text)
for t in doc:
if t.lower_ in [' go ', 'golang']:
if t.pos_ != 'VERB':
if t.dep_ == 'pobj':
return True
return False
g = (title for title in titles if has_golang(title))
[next(g) for i in range(10)]
#这是错误
StopIteration Traceback (most recent call last)
<ipython-input-56-862339d10dde> in <module>
9
10 g = (title for title in titles if has_golang(title))
---> 11 [next(g) for i in range(10)]
<ipython-input-56-862339d10dde> in <listcomp>(.0)
9
10 g = (title for title in titles if has_golang(title))
---> 11 [next(g) for i in range(10)]
StopIteration:
据我所做的研究,我认为这可能是一个错误
我想做的就是获得满足 3 个“如果”条件的标题
解决方案
这StopIteration
是调用next()
耗尽迭代器的结果,即g
产生少于 10 个结果。您可以从help()
函数中获取此信息。
help(next)
Help on built-in function next in module builtins:
next(...)
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
编辑
你has_golang
的不正确。第一个测试总是False
因为nlp
标记单词,即修剪前导和尾随空格。尝试这个:
def has_golang(text):
doc = nlp(text)
for t in doc:
if t.lower_ in ['go', 'golang']:
if t.pos_ != 'VERB':
if t.dep_ == 'pobj':
return True
return False
我通过找到一个应该导致True
from的标题来解决这个问题has_golang
。然后我运行以下代码:
doc = nlp("Making a Simple FileServer with Go and Localhost Refused to Connect")
print("\n".join(str((t.lower_, t.pos_, t.dep_)) for t in doc))
('制作','动词','csubj') ('a', 'DET', 'det') ('简单','PROPN','复合') ('文件服务器','PROPN','dobj') ('with', 'ADP', 'prep') ('去','PROPN','pobj') ('和','CCONJ','cc') ('localhost','PROPN','conj') ('拒绝','动词','根') (“到”、“部分”、“辅助”) ('连接','动词','xcomp')
然后看('go', 'PROPN', 'pobj')
,很明显PROPN不是动词,pobj是pobj,所以问题必须出在token上:go,特别是"go"
not " go "
。
原始回复
如果您只想要满足 3 个if
条件的标题,请跳过生成器:
g = list(filter(has_golang, titles))
如果您需要生成器但也想要一个列表:
g = (title for title in titles if has_golang(title))
list(g)
推荐阅读
- decimal - QGIS不会将y和x坐标更改为十进制度
- python - 根据日期范围合并行
- pyspark - 找到“匹配”时打印上一行:Pyspark
- python - 拆分数据时出现问题:KeyError: "None of [Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype='int64')] 在[列]"
- node.js - TypeError: _babel_runtime_corejs2_core_js_promise__WEBPACK_IMPORTED_MODULE_17___default.a.allSettled 不是函数
- html - 响应式 iframe,但没有声音
- c# - .NET 4.6 中的 OpenIddict 3 不可用令牌验证
- wix - WiX 2716 错误:无法为组件创建随机子组件名称
- javascript - 如何将列表更改为具有布尔值的字典
- android - 如果 logcat 没有显示原因,我如何找出我的应用程序崩溃的原因?