0

From this answer I learned how can I count occurrences of any character in a string using a map,

Count Vowels in String Python

Here is my code which counts occurance of any vowels in a string,

name = "maverick dean"
vowels = 'aeiou'
x = list(map(name.lower().count, 'aeiou'))

As you can see I used list to put each value of a map in a list.

Which gives this output,

[2, 2, 1, 0, 0]

My desire output is

[ "a:2", "e:2", "i:1", "o:0", "u:0" ]

Now I understand I can use for loop to do it, but is there any other way to map output of x directly so that it shows alongside the actual vowel?

MaverickD
  • 1,477
  • 1
  • 13
  • 27

2 Answers2

2

You can use lambda function for this:

x = list(map(lambda v: "{}:{}".format(v, name.lower().count(v)), vowels))
print(x)
# ['a:2', 'e:2', 'i:1', 'o:0', 'u:0']
Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
1

You can use a list comprehension

x = ["%s:%d" % (letter, name.lower().count(letter)) for letter in vowels]

or you could even use a zip onto the original list i.e.

x = ["%s:%d" % (l,c) for l, c in zip(vowels, map(name.lower().count, vowels))]