2
def get_count(input_str):
    
    return input_str.count("a") + input_str.count("e") + input_str.count("i") + input_str.count("o") + input_str.count("u")

3 Answers 3

1
s = "hi what's up by there?"

lookup = ["e", "i", "u"]

print([s.count(x) for x in lookup])

Output:

[2, 1, 1]

In a function:

def lookup_count(s, lookup):
    return [s.count(x) for x in lookup]
Sign up to request clarification or add additional context in comments.

Comments

0

You have two strings: one that is your target, and one that is your search set. You have to loop through one of them and search the other. Which way is better depends on the length of the strings.

Comments

0

You can simplify this in two ways:

First:

def get_count(input_str, target_str='aeiou'):
    return sum(map(input_str.count,target_str))

You can also use collections.Counter

from collections import Counter
def get_count(input_str, target_str='aeiou'):
    cnt = Counter(input_str)
    return sum([cnt[chr] for chr in target_str])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.