i'm doing tweepy/django/nltk project have list update searched tweets. here's part i'm having problem:
query = 'happy' max_tweets=5 search_results = {} sentiments = {} sentilist = [] status in tweepy.cursor(api.search, q=query).items(max_tweets): search_results[status.text] = unicode(status.text) search_results[status.text] = search_results[status.text].replace('|', ' ') search_results[status.text] = search_results[status.text].replace('\n', ' ') print(senti.linearsvc10(status.text)) sentiments['tweet'] = unicode(search_results[status.text]) sentiments['sentiment'] = senti.linearsvc10(unicode(status.text)) sentilist.append(sentiments) print('inloop sentiments') print sentiments print('inloop sentilist') print sentilist print('sentiments') print sentiments print('sentilist') print sentilist
basically, sentiments equal to
{'tweet': 'actual tweet here', 'sentiment': 'pos'}
so each run of loop, want sentiments append list, end of that, have 5 different objects in list. happens each append sentilist, changes each item in list last object appended. example, following individual sentiments objects:
{'tweet': 'tweet1', 'sentiment': 'pos'} {'tweet': 'tweet2', 'sentiment': 'neg'} {'tweet': 'tweet3', 'sentiment': 'neg'} {'tweet': 'tweet4', 'sentiment': 'pos'} {'tweet': 'tweet5', 'sentiment': 'neg'}
when appending sentilist should be:
[{'tweet': 'tweet1', 'sentiment': 'pos'}, {'tweet': 'tweet2', 'sentiment': 'neg'}, {'tweet': 'tweet3', 'sentiment': 'neg'}, {'tweet': 'tweet4', 'sentiment': 'pos'}, {'tweet': 'tweet5', 'sentiment': 'neg'}]
but instead becomes:
[{'tweet': 'tweet5', 'sentiment': 'neg'}, {'tweet': 'tweet5', 'sentiment': 'neg'}, {'tweet': 'tweet5', 'sentiment': 'neg'}, {'tweet': 'tweet5', 'sentiment': 'neg'}, {'tweet': 'tweet5', 'sentiment': 'neg'}]
other parts of codes work , feel there's simple solution still can't figure out.
you need make new dictionary sentiments
in each loop:
for status in tweepy.cursor(api.search, q=query).items(max_tweets): sentiments = {}
you override values in same dictionary again , again , append same dictionary in each loop. therefore, see values last dictionary update in entries in list sentilist
.
Comments
Post a Comment