Write a Python program that counts the number of times each

Write a Python program that counts the number of times each digit occurs, the number of times a whitespace occurs, and the number of other characters.

It returns a list of 12 elements (a count for each of the ten digits, plus whitespace, plus other characters).

It is possible to write it in two lines of code, but not required

def mycount(str):

mycount = [ str.count(c) for c in \"0123456789 \\t\ \" ]

return

Solution


def mycount(str):
   count = [0,0,0,0,0,0,0,0,0,0,0,0]
   for i in range(len(str)):
       # if str[i] is digit, increment count of that index.
       if(str[i].isdigit()):
           count[int(str[i])] += 1
       elif(str[i]==\" \"):
           count[10] += 1
       else:
           count[11] += 1
   return count
print mycount(\"123ljkf567abc\ \")

\"\"\"
sample output

[0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 8]
\"\"\"

Write a Python program that counts the number of times each digit occurs, the number of times a whitespace occurs, and the number of other characters. It return

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site