我有一个获取大量不同推文的for循环,在该循环中有一个带有4个变量的情感分析算法,我需要将每个推文+这些变量存储在另一个变量或列表中,所以我可以在一个变量或列表中重用它html页面。
为了更清楚,这是我的代码:
for tweet in tweepy.Cursor(api.search,q='abc').items(num_tweets):
result = some.stuff(tweet.text)
print (result)
输出
{'openness': 0.4745553153, 'extraversion': 0.5485006308, 'agreeableness': 0.4339935487, 'conscientiousness': 0.5115956027}
{'openness': 0.6179026878, 'extraversion': 0.7166606274, 'agreeableness': 0.3913384864, 'conscientiousness': 0.4469014314}
我试图将每个值存储在一个变量中(在循环内):
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 ('---')
输出
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
---
我的问题是在列表中存储每个推文+4个变量的最佳方法是什么?
我的尝试是:
results = []
results.extend( final, op, ex, ag, co)
#results.append( final, op, ex, ag, co)
print (results)
TypeError:extend()只接受一个参数(给定5个)
还有这种技术,但坦白说,我迷路了:
pers = [(result['openness']), (result['extraversion']),(result['agreeableness']), (result['conscientiousness'])]
results.append((final, pers))
print (results)
上次尝试没有错误。
答案 0 :(得分:1)
这可能会有所帮助。
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 )