pythoncountnumbersdigitskeypad

Count number's digits following by line


I have the number 444333113333 and I want to count every different digit in this number.

4 is 3 times

3 is 3 times

1 is 2 times

3 is 4 times

What I am trying to do is make a script that translates phone keypad taps to letters like in this photo https://www.dcode.fr/tools/phone-keypad/images/keypad.png if I press 3 times number 2, then the letter is 'C'

I want to make a script with it in python,but I cannot...


Solution

  • Using regex

    import re
    pattern = r"(\d)\1*"
    
    text = '444333113333'
    
    matcher = re.compile(pattern)
    tokens = [match.group() for match in matcher.finditer(text)] #['444', '333', '11', '3333']
    
    for token in tokens:
        print(token[0]+' is '+str(len(token))+' times')
    

    Output

    4 is 3 times
    3 is 3 times
    1 is 2 times
    3 is 4 times