I am fairly new to programming and have been dabbling with Swift and Python. Please be patient thank you!!
I am trying to use python to read tweets from Twitter (via Twitter API) and am trying to retrieve data for what tickers are being talked about on my home page and how many times they are being mentioned. When I run the program and merge the lists, the tweets with no stock tickers come up as empty []. I have the output of my code within the image below.
For instance, if ['PYPL'] came up 2 times, ['TSLA'] came up 4 times, and ['AAPL'] came up 12 times, I am hoping to have an output that says something like:
AAPL: 12 TSLA: 4 PYPL: 3
Thank you in advance!!
THIS IS THE CODE (excluding the twitter/API keys):
feed = API.home_timeline(count = 200)
array_of_tickers = []
for tweet in feed:
iden = str(tweet.id) + '-' + tweet.text
tickers = re.findall(r'[$][A-Za-z][\S]*', iden)
array_of_tickers.append(tickers)
print(tickers)
array_of_tickers.remove("")
print(array_of_tickers)
If there's no tickers mentioned in the tweet, skip it. Keep track of mention counts using a dict
.
feed = API.home_timeline(count = 200)
array_of_tickers = []
counts = {}
for tweet in feed:
iden = str(tweet.id) + '-' + tweet.text
tickers = re.findall(r'[$][A-Za-z][\S]*', iden)
# do this if you only want to count each ticker once in a tweet
tickers = set(tickers)
if len(tickers) < 1:
continue
array_of_tickers.append(tickers)
for ticker in tickers:
ticker = ticker.upper()
counts[ticker] = counts[ticker] + 1 if ticker in counts else 1
sorted_tickers = sorted(counts.items(), key=lambda x: x[1], reverse=True)
print(', '.join([f'{k}: {v}' for k,v in sorted_tickers]))