j44mvh
j44mvh

Reputation: 21

Store each item in for loop in a separate variable

I have a for loop that fetch a number of different tweets, inside that loop there's a sentiment analysis algorithm with 4 variables, I need to store each tweet + these variables in another variable or list, so I can reuse it on a html page.

To make it more clear here's my code:

for tweet in tweepy.Cursor(api.search,q='abc').items(num_tweets):
    result = some.stuff(tweet.text)

    print (result)

Output

{'openness': 0.4745553153, 'extraversion': 0.5485006308, 'agreeableness': 0.4339935487, 'conscientiousness': 0.5115956027}
{'openness': 0.6179026878, 'extraversion': 0.7166606274, 'agreeableness': 0.3913384864, 'conscientiousness': 0.4469014314}

I tried to store each value in a variable (inside the loop):

print (tweet.text)

op = (result['openness'])
ex = (result['extraversion'])
ag = (result['agreeableness'])
co = (result['conscientiousness'])

print ("Openness is:", op)
print ("Extraversion is:", ex)
print ("Agreeableness is:", ag)
print ("Conscientiousness is:", co)

print ('---')

Output

Lorem ipsum dolor
Openness is: 0.46639431530000003
Extraversion is: 0.5582758198000001
Agreeableness is: 0.479510345
Conscientiousness is: 0.47180472980000004
---
Sit amaet varch colon
Openness is: 0.4829023074
Extraversion is: 0.5457794199
Agreeableness is: 0.4973260269
Conscientiousness is: 0.49511686720000003
---

My question is what's the best approach to store each tweet + 4 variables in a list?

My attempt is:

results = []
results.extend( final, op, ex, ag, co)
#results.append( final, op, ex, ag, co)
print (results)  

TypeError: extend() takes exactly one argument (5 given)

There's also this technique but to be frank, I'm lost:

pers = [(result['openness']), (result['extraversion']),(result['agreeableness']), (result['conscientiousness'])]
results.append((final, pers))

print (results) 

There's no error in the last attempt.

Upvotes: 0

Views: 115

Answers (1)

Abraham Tugalov
Abraham Tugalov

Reputation: 1912

This may help.

results = []

for tweet in tweepy.Cursor(api.search,q='abc').items(num_tweets):
    result = some.stuff(tweet.text)

    print (result)

    op = (result['openness'])
    ex = (result['extraversion'])
    ag = (result['agreeableness'])
    co = (result['conscientiousness'])
    results.append( [tweet.text, op, ex, ag, co] )

print( results )

Upvotes: 1

Related Questions