简体   繁体   中英

Multiple Search Terms in List

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.

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!

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%

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM