Search code examples
pythonarrayssearchtwittertweepy

How can I find strings within an array based on keywords in another array in python?


So I'm using tweepy to initially extract tweets depending on a username entered. I then store these tweets in an array. I then want to filter through the tweets depending on a list of keywords. I have tried a few things for this and my best two attempts have given results but not the type I'm looking for. I was wondering if anyone could point me in the right direction? this is what I have so far...

1: - The issue with this one is that it only takes the string and prints it if it only includes the keyword and nothing else. 'Keywords' is just a list of words I'm using to filter tweets. Something like this is exactly what I need, however I can't seem to figure out how to get it to include tweets with other text but the keyword?

for status in tweepy.Cursor(api.user_timeline, screen_name='@'+username).items(50): # only iterate through first 50 statuses
    tweet_list.append(status._json['text']) 
tweet_list2 = [k for k in tweet_list if k in keywords] #new array for tweets including keywords
print tweet_list2

2:- The issue here is that I can only get it to work for the singular string specified, e.g. 'good' in this case. However, it takes the and prints out the whole string that the keyword is included in (ideally what I want). It doesn't just take strings that ONLY include the specified string, it takes all that include it.

tweet_list2 = [k for k in tweet_list if 'good' in k]
print tweet_list2

I feel like I'm pretty close here but can't quite hit the nail on the head. I appreciate any help!


Solution

  • Try this out:

    tweet_list2 = [k for k in tweet_list if any(word in k for word in keywords)]