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.
1
solved Coding a function in python wihch count the number of occurances [closed]