为什么我会得到;属性错误:';str';对象没有属性';追加'&引用;用Python?
我试图用500个不同的TXT生成一个潜在的Dirichlet分配模型。 我的代码的一部分如下:为什么我会得到;属性错误:';str';对象没有属性';追加'&引用;用Python?,python,model,lda,Python,Model,Lda,我试图用500个不同的TXT生成一个潜在的Dirichlet分配模型。 我的代码的一部分如下: from gensim.models import Phrases from gensim import corpora, models bigram = Phrases(docs, min_count=10) trigram = Phrases(bigram[docs]) for idx in range(len(docs)): for token in bigram[docs[idx]]
from gensim.models import Phrases
from gensim import corpora, models
bigram = Phrases(docs, min_count=10)
trigram = Phrases(bigram[docs])
for idx in range(len(docs)):
for token in bigram[docs[idx]]:
if '_' in token:
# Token is a bigram, add to document.
docs[idx].append(token)
for token in trigram[docs[idx]]:
if '_' in token:
# Token is a bigram, add to document.
docs[idx].append(token)
它给出了以下错误:
File ".../scriptLDA.py", line 74, in <module>
docs[idx].append(token)
AttributeError: 'str' object has no attribute 'append'
文件“../scriptLDA.py”,第74行,在
文档[idx]。追加(令牌)
AttributeError:“str”对象没有属性“append”
有人能帮我修一下吗?
谢谢 欢迎来到Stackoverflow Python告诉您docs[idx]不是一个列表,而是一个字符串。因此,它没有可供您调用的append()方法
>>> fred = "blah blah"
>>> fred.append("Bob")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'
>>> elsie = [1,2,3,4]
>>> elsie.append(5)
>>> elsie
[1, 2, 3, 4, 5]
>>> type(fred)
<class 'str'>
>>> type(elsie)
<class 'list'>
>>>
如果更复杂,那就不同了。append()用于向数组中添加元素,而不是连接字符串。
你可以做:
a=“string1”
a=a+“string2”
或:
a=[1,2,3,4]
a、 附加(5)
>>> ginger = fred + "Another string"
>>> ginger
'blah blahAnother string'