Goal
Goal

Reputation: 87

How could i use the count() function in my function?

I have made a function which to filtering the vowels in a string.

def number_of_vowels(stringPara):
    vowels = ['u', 'e', 'o', 'a', 'i']
    return list(filter(lambda x: x in vowels, stringPara))
print(number_of_vowels("Technical University"))    

Now I need to count every single vowel the string contain by count() function. But I have no idea how to use it when I have the lambda function.

Upvotes: 0

Views: 88

Answers (4)

user12596436
user12596436

Reputation:

How about this?

from collections import Counter


def numberOfVowels(param):
    vowels = {'a', 'e', 'i', 'o', 'u'}
    result = list(filter(lambda x: x in vowels, param))
    return result, dict(Counter(result))


print(numberOfVowels('Technical University'))


# Output:
# (['e', 'i', 'a', 'i', 'e', 'i'], {'e': 2, 'i': 3, 'a': 1})

Upvotes: 0

funnydman
funnydman

Reputation: 11326

You could use Counter for this:

from collections import Counter

counter = Counter("Technical University")
vowels = ['u', 'e', 'o', 'a', 'i']

print({k: v for k, v in counter.items() if k in vowels})
# {'e': 2, 'i': 3, 'a': 1}

Upvotes: 1

Giannis Clipper
Giannis Clipper

Reputation: 707

It returns the total count and count per vowel:

def number_of_vowels(stringPara):
    vowels = ['u', 'e', 'o', 'a', 'i']
    f = list(filter(lambda x: x in vowels, stringPara))
    return(len(f), {v: f.count(v) for v in vowels if f.count(v)>0})

print(number_of_vowels("Technical University"))

Upvotes: 0

gioaudino
gioaudino

Reputation: 575

def number_of_vowels(stringPara):
    vowels = ['u', 'e', 'o', 'a', 'i']
    return len(list(filter(lambda x: x in vowels, stringPara)))
print(number_of_vowels("Technical University"))

Is this what you're looking for?

Upvotes: 1

Related Questions