问题
I have a following mongoDBschema-
ObjectIDs are multiple entries in the database ,in which the further data is stored.
ObjectId("a")
posts
[0]
comments
[0]
comment
fb_id
[1]
[2]
[3]
[4]
ObjectId("b")
ObjectId("c")
I am trying to cumulatively add the sentiment value,I am assigning to the dictionary for each comment corresponding to every fb_id.I tried using counter method but it's values are emptying before entering another id's comment automatically. Earlier,I asked this question-Cumulatively add values to python dictionary and the output ,I received is below-
Counter({'100002204201460': 0.0})
Counter({'100002204201460': 0.0, '100025548426588': -0.592})
Counter({'1490834948': 0.5792,
'100002204201460': 0.0,
'100025548426588': -0.592})
Counter is emptying itself before entering into another comment of same post.
My implementation-
for doc in result:
dict_of_comments = Counter()
for post in doc['posts']: # traverse each user's post
if 'comments' in post:
for com in post['comments']:
s=com['comment']
vs=analyzer.polarity_scores(s) //sentiment analyser
dict_of_comments[com['fb_id']] += vs['compound']
pprint.pprint(dict_of_comments)
How to do it correctly,I just want to cumulatively add values without losing any of the values as currently ,I am loosing using Counter.
Input in one of the objectID
{
"_id" : ObjectId("5b7fa2a745d12b10324d3a01"),
"date_time" : "24, Aug 2018 11:44",
"original_query" : "sanjeevnewar",
"posts" : [
"comments" : [
{
"comment" : "आशीष जी को कोटि कोटि प्रणाम।",
"commentator" : "Amrit Kumar Deb",
"fb_id" : "100002204201460"
},
{
"comment" : "Double standard is not going to work Stop ogling over Muslim women",
"commentator" : "Akif Khan",
"fb_id" : "100025548426588"
}
]
]
}
Output ,if a standard dictionary is used without using Counter is as below
{'100002204201460': 0.0}
{'100002204201460': 0.0, '100025548426588': -0.296}
This is just a sample output, not all values are included.
But the issue is if my program encounters a same fb_id value(which is 100002204201460
) in later posts, then it will just rewrite the dictionary with new values rather than adding values to it.
How to do it?
来源:https://stackoverflow.com/questions/52184270/cumulatively-add-values-to-python-dictionary-without-using-counter