Having trouble finding a python solution to matching elements of one list against elements in another list without a whole pile of “for” and “if” loops. I’m hoping to find a better way to do this. I have some big iterating loops that go through multiple lists to perform matches. On a match, I want elements of the list removed. Here are two examples:
def score_and_retweet(auth):
api = tweepy.API(auth)
for tweet in api.home_timeline(count=100, include_rts=0):
for goodword in tweet_whitelist:
if goodword in tweet.text and tweet.retweet_count >= 2:
try:
api.retweet(tweet.id_str)
except tweepy.error.TweepError:
error_id = tweet.id_str
and
t = time.localtime()
if t.tm_hour is 14 and (t.tm_wday is 1 or t.tm_wday is 4):
htmlfiles = glob.glob(html_file_dir+'/*.html')
for file in htmlfiles:
for badword in filename_badwords:
if badword in file:
try:
htmlfiles.remove(file)
except ValueError:
error = "already removed"
Not sure how much it would change in terms of performance, but you could write a filter function
For example in the second case (if you’re looking for exact matches)
Then use:
The advantage this has over set intersection is you can make the filter function as complex as you want (you have multiple conditions in your first example)