我已经阅读了默认dict中的所有脚本以及此处的所有帖子。我相信我的语法是正确的。
influenceDict = defaultdict(list)
填写所有推文中的所有标签
稍后,我为大多数字典键附加了ALOT浮点值,1000多个列表条目。我在第47行得到了我的错误,如下所示。
def addInfluenceScores(hashtagArr,numFollowers,numRetweets, influenceDict):
influenceScore = float(0)
if numFollowers == 0 and numRetweets != 0:
influenceScore = numRetweets + 1
elif numFollowers == 0 and numRetweets == 0:
influenceScore = 0
else:
influenceScore = numRetweets / numFollowers
print "Adding influence score %f to individual hashtags" % (influenceScore)
for tag in hashtagArr:
tID = tag2id_map[tag]
print "Appending ",tID,tag
# if not influenceDict.has_key(tID):
# influenceDict[tID] = list()
# influenceDict[tID].append(influenceScore)
# else:
# influenceDict[tID].append(influenceScore)
influenceDict[tID].append(influenceScore) **#LINE 47 I GET THE ERROR HERE**
for i in range(len(hashtagArr)):
for j in range(i+1, len(hashtagArr)):
tID1 = tag2id_map[hashtagArr[i]]
tID2 = tag2id_map[hashtagArr[j]]
if(tID2 < tID1): #ensure alpha order to avoid duplicating (a,b) and (b,a)
temp = tID1
tID1 = tID2
tID2 = temp
print "Appending ",tID1, hashtagArr[j],tID2,hashtagArr[i]
# if not influenceDict.has_key((tID1, tID2)):
# influenceDict[(tID1, tID2)] = list()
# influenceDict[(tID1, tID2)].append(influenceScore)
# else:
# influenceDict[(tID1, tID2)].append(influenceScore)
influenceDict[(tID1, tID2)].append(influenceScore)
The program runs for a while, and it actually does append values (or so I think) and then I get this error:
Traceback (most recent call last):
File "./scripts/make_id2_influencescore_maps.py", line 158, in <module
processTweets(tweets, influenceDict)
File "./scripts/make_id2_influencescore_maps.py", line 127, in processTweets
addInfluenceScores(hashtags, numFollowers,numRetweets, influenceDict)
File "./scripts/make_id2_influencescore_maps.py", line 47, in addInfluenceScores
influenceDict[tID].append(influenceScore)
AttributeError: 'float' object has no attribute 'append'
我在想这个列表只是在内存中最大化了。也许你们可以看到我不喜欢的东西。我试图循环浏览一个推文文件,每次我看到我想要将分数附加到与之关联的列表中的主题标签。这样,当我完全读完文件时,我可以取出列表中所有分数的平均值。谢谢。
答案 0 :(得分:1)
我在想这个列表只是在内存中最大化了。
如果你的错误是
,我可以向你保证AttributeError: 'float' object has no attribute 'append'
问题不在此处显示的代码中,因为influenceDict
是一个参数,您显然已将其中一个键设置为指向代码中其他位置的float
值。仅仅因为它是defaultdict(list)
并不能阻止这种情况发生。