属性错误:"列表"对象在创建 n-gram 时没有属性"str"



1.我想在我的数据集上应用n-gram。我创建了代码,并想在我的数据集上进行测试:

def generate_N_grams(text,ngram=1):
words=[word for word in text.split(" ").str if word not in set(stopwords.words('english'))]  

temp=zip(*[words[i:] for i in range(0,ngram)])
ans=[' '.join(ngram) for ngram in temp]
return ans
news['text_process_1gram'] = news['text_process'].str.split(',')
news['text_process_1gram'] = news['text_process'].astype(str)
news['text_process_1gram'] = news['text_process'].apply(lambda x:generate_N_grams(x, 1))

2.然后我得到错误消息:AttributeError:"list"对象没有属性"str"。我为什么得到这些信息?

example = ['1', '2', '3']
print(example.str)

这将导致AttributeError: 'list' object has no attribute 'str',因为列表没有属性str


words = [word for word in text.split(',') if word != 'whatever']

text.split(" ")将产生一个字符串列表,您不必确保word是一个字符串。

最新更新