Search code examples
pythonsubstring

Searching for 2 substrings in a list of strings


I am attempting to count the instances of substring in a list, but I am confused on how I could count if a value has two different substrings in the same string in a list. This is my current program, and line 11 is where I am unsure what to do.

import json

FILENAME = "superbowltweets.json"

def main():
    with open(FILENAME) as file:
        data = json.load(file)
    tweets = [tweet["full_text"] for tweet in data]
    rams_count = has_rams("rams", tweets)
    pats_count = has_pats("pat", tweets)
    both_count = has_both("pats rams", tweets)

def has_rams(sub_str, tweets):
    rams_count = len([s for s in tweets if sub_str in s])
    print("There are " +str(rams_count) +  " tweets about the Rams")
    return rams_count

def has_pats(sub_str, tweets):
    pats_count = len([s for s in tweets if sub_str in s])
    print("There are " +str(pats_count) + " tweets about the Patriots")
    return pats_count

def has_both(sub_str, tweets):
    both_count = len([s for s in tweets if sub_str in s])
    print("There are " + str(both_count) + " tweets that mention both")

if  __name__=="__main__":
    main()

And here is a small snippet of the tweet list:

["lets go rams!", "patriots suck and the rams are going to win!", "I hate tom brady", "rams should go back to saint louis"]

I want to check if a value in the list tweets contains both "rams" and "pats", then assign that number to a variable. I would like to keep my current functions.


Solution

  • You can modify has_both to:

    def has_both(sub_str1, sub_str2, tweets):
        both_count = len([s for s in tweets if (sub_str1 in s) and (sub_str2 in s)])
        print("There are " + str(both_count) + " tweets that mention both")
    

    Correct, should be and.