我在这里使用了 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 个“如果”条件的标题