Multiple Search Terms in List

Question:

I’m looking to count whether eth or btc appear in listy

searchterms = ['btc', 'eth']
listy = ['Hello, my name is btc', 'hello, my name is ETH,', 'i love eth', '#eth is great', 'barbaric tsar', 'nothing']

cnt = round((sum(len(re.findall('eth', x.lower())) for x in listy)/(len(listy)))*100)
print(f'{cnt}%')

The solution only looks for eth. How do I look for multiple search terms?

Bottom line: I have a list of search terms in searchterms. I’d like to see if any of those appear in listy. From there, I can perform a percentage of how many of those terms appear in the list.

Asked By: getintoityuh

||

Answers:

I would say instead of complicating the problem and using re, use a simple classic list comprehension.

listy = ['Hello, my name is btc', 'hello, my name is ETH,', 'i love eth', '#eth is great', 'barbaric tsar', 'nothing']
print(len([i for i in listy if 'btc' in i.lower() or 'eth' in i.lower()]) * 100 / len(listy))

It improves the readability and the simplicity of the code.

Let me know if it helps!

Answered By: VMSM

you need to use the pipe "|" betwwen the values you want to search. In your code change re.findall('eth', x.lower() by re.findall(r"(eth)|(btc)", x.lower()

listy = ['Hello, my name is btc', 'hello, my name is ETH,', 'i love eth', '#eth is great', 'barbaric tsar', 'nothing']

cnt = round((sum(len(re.findall(r"(eth)|(btc)", x.lower())) for x in listy)/(len(listy)))*100)
print(f'{cnt}%')

67%
Answered By: Lucas M. Uriarte

a bit more of code is giving a good readability. also adding additional words to search for, just needs to add them to the list search_for. for counting it uses a defaultdict.

listy = ['Hello, my name is btc', 'hello, my name is ETH,', 'i love eth', '#eth is great', 'barbaric tsar', 'nothing']
my_dict = defaultdict(int)
search_for = ['btc', 'eth']
for word in listy:
    for sub in search_for:
        if sub in word:
            my_dict[sub] += 1
print(my_dict.items())
Answered By: lroth
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.