-5

I am a beginner in python and I wish coding a function having a variable number of parameters. This function must count the number of occurance of each character existing in all input strings. let's rename this function as carCompt.

For example :

carCompt("Sophia","Raphael","Alexandre")

the result should be:

{'A':5,
 'D':1,
 'E':3,
 'H':2,
 'L':1,
 'N':1,
 'O':1,
 'P':2,
 'R':2,
 'S':1,
 'X':1}

thank you for help!!

user8810618
  • 115
  • 11
  • 3
    What have you tried? Where are you stuck? Unfortunately, as written, this is a "write code for me" question. And this isn't a free code-writing service. Please edit your question accordingly. – David Makogon Jan 01 '18 at 19:48
  • Try using dictionary as your starting point. – dmitryro Jan 01 '18 at 19:49
  • possible duplicate: https://stackoverflow.com/questions/991350/counting-repeated-characters-in-a-string-in-python – eyllanesc Jan 01 '18 at 19:59

1 Answers1

1

Use the collections module to use the Counter function. Such as:

import collections

def carCompt(*args):
    return collections.Counter("".join(map(str.upper, args)))

This will return

{'A':5,
 'D':1,
 'E':3,
 'H':2,
 'L':1,
 'N':1,
 'O':1,
 'P':2,
 'R':2,
 'S':1,
 'X':1}

If you want it to be case sensitive then leave it like:

import collections

def carCompt(*args):
    return collections.Counter("".join(args))

which will return

{'a': 4, 'e': 3, 'p': 2, 'h': 2, 'l': 2, 'S': 1, 'o': 1, 'i': 1, 'R': 1, 'A': 1, 'x': 1, 'n': 1, 'd': 1, 'r': 1}

Also I suggest changing the function name from carCompt to car_compt as per PEP8.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96