Python错误:';str';对象不支持项分配
我对Python相当陌生,我一直在寻找这个错误的答案,但我没有足够的经验来准确地看到我的错误所在——这可能是一些非常基本的问题。Python错误:';str';对象不支持项分配,python,Python,我对Python相当陌生,我一直在寻找这个错误的答案,但我没有足够的经验来准确地看到我的错误所在——这可能是一些非常基本的问题。 我正在做一个项目,根据作者在文本中使用的词语来确定作者。我将这些词添加到每个作者的词典中,以该词为关键字,值是该词在该作者的文本中出现的次数。我还创建了所有作者的词汇表,并使用这些词汇计算概率。一开始这很好。 当我加入k-fold交叉验证时,我的问题就来了,因为我的语料库不是特别大。我遍历作者姓名列表,该列表与我分配给他们的空字典的姓名相匹配。一旦我提取了我想要的文件
我正在做一个项目,根据作者在文本中使用的词语来确定作者。我将这些词添加到每个作者的词典中,以该词为关键字,值是该词在该作者的文本中出现的次数。我还创建了所有作者的词汇表,并使用这些词汇计算概率。一开始这很好。
当我加入k-fold交叉验证时,我的问题就来了,因为我的语料库不是特别大。我遍历作者姓名列表,该列表与我分配给他们的空字典的姓名相匹配。一旦我提取了我想要的文件,我想将清理/解析后的文本添加到字典中,但是我得到了上面的错误,它指的是我字典fn中的行author[word]=1,我在下面最后一行代码中调用了它。从我阅读的其他答案来看,这与str是不可变的有关,但我不知道如何将这些答案应用到我的问题中。非常感谢你的帮助! Ps我知道有一些库等可以完成所有这些工作,但整个项目的想法是构建我自己的模型,并将其与其他模型进行比较
path = "C:\\......\The Letters\\"
#create an empty vocab set
vocab = set()
stop = stopwords.words('english')
snowball = SnowballStemmer('english')
#create empty dictionary for each author
AuthorA = {}
AuthorB = {}
AuthorC = {}
authorList = ["AuthorA","AuthorB","Authorc"]
#function to preprocess the words. Opens & reads file, removes non alphabet
#characters, converts to lowercase, and tokenizes
def cleanText(path,author,eachfile):
f= open(path+author+"\\"+eachfile, "r")
contents = f.read()
strip = re.sub('[^a-zA-Z]',' ',contents)
lowerCase = strip.lower()
allwords = lowerCase.split()
return allwords
#function to add words to the vocabulary set
def createVocab(allwords):
for word in allwords:
if len(word)>= 4:
vocab.update(allwords)
return
#function to add words to author dictionary and count occurrences of each word
def dictionary(allwords, author):
for word in allwords:
if len(word)>= 4:
if word in author:
author[word]= author[word]+1
else:
author[word]= 1
return
def main():
global authorList
global path
global vocab
global AuthorA
global AuthorB
global AuthorC
for author in authorList:
#filename and path
listing = os.listdir(path+author)
#specify parameters for k fold validation
#split into 10 folds and take a file form each fold
#repeat for until the entire directory has been split
folds = 10
subset_size = len(path+author)/folds
for i in range(folds):
#use these files to train the model
current_train = listing[:i*subset_size:]+listing[(i+1)*subset_size:]
#use these files to test the model
current_test = listing[i*subset_size:][:subset_size]
#iterate through the files selected by current_train variable
for eachfile in current_train:
#call function to parse text
allwords = cleanText(path,author,eachfile)
#call fn to add words to dictionary
dictionary(allwords, author)
#call fn to add words to vocab
createVocab(allwords)
您正在向字典函数传递变量author的字符串。authorList:中author的循环顶部是迭代字符串列表,而不是dict集合
authorList=[“AuthorA”、“AuthorB”、“Authorc”]
您希望将dict集合传递给函数。希望有帮助 您在哪一行收到错误?请熟悉和。正如现在所说的,这个问题对于SO标准来说太广泛了。此外,还要注意缩进。在Python中,缩进是语法的一部分,在您的代码中,缩进看起来很可疑。错误是当我在最后第二行调用dictionary函数时,引用了else:author[word]=1。我想D.Cam已经回答了,我现在只需要找出收藏!谢谢,我试试看。我只需要弄清楚如何使用dict集合!你的用法很正确。不使用列表,您可以始终使用dict.
authors={“AuthorA”:AuthorA,“AuthorB”:AuthorB,“AuthorC”:AuthorC}
,然后您可以使用author,author\u dict in authors.iteritems()对,这就是我最终得出的结论。我创建了一个嵌套字典,这样我就可以存储每个作者的单词,并且我正在迭代键。谢谢你的帮助!