'Applying mapping to strings

I have the following function that counts the number of consonants for each word:

def number_of_consonants(words):
    
    consonant_list = [len([letter for letter in word if letter.lower() not in 'aeiou']) \
            for word in words.split()]
    return print(consonant_list)

Suppose I have a simple text

text= "eventually renamed Star Wars Episode IV: A New Hope years later"

Applying the function generates the desired result

number_of_consonants(text)
[6, 4, 3, 3, 3, 2, 0, 2, 2, 3, 3]

However, if when I try to use the mapping function on the same text, I get an entirely different output. For example


result = map(number_of_consonants, text)
list(result)

sample output generated:
[0]
[1]
[0]
[1]
...
None, 
None

How do I get the same output as the function output?

Thanks



Solution 1:[1]

A string is a sequence, so when you call map, you're applying the function to each character in the string.

You need to perform the splitting beforehand, so that you perform the map on the list of words.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ndc85430