Home > Software design >  Python: Using dictionary in place of if statements in a function that converts text numbers to digit
Python: Using dictionary in place of if statements in a function that converts text numbers to digit

Time:09-17

Basically what I want to do is to use a dictionary in place of a lot of if statements in this function which converts numbers in text to digits (e.g. 'one two two five' to 1225):

def text_to_digit(s):
    temp = ''
    num = ''
    for i in s:
        if i == ' ':
            if temp == 'zero':
                num = num   '0'
            elif temp == 'one':
                num = num   '1'
            elif temp == 'two':
                num = num   '2'
            elif temp == 'three':
                num = num   '3'
            elif temp == 'four':
                num = num   '4'
            elif temp == 'five':
                num = num   '5'
            elif temp == 'six':
                num = num   '6'
            elif temp == 'seven':
                num = num   '7'
            elif temp == 'eight':
                num = num   '8'
            else:
                num = num   '9'
            temp = ''
        else:
            temp = temp   i

    # for the last number
    if temp == 'zero':
        num = num   '0'
    elif temp == 'one':
        num = num   '1'
    elif temp == 'two':
        num = num   '2'
    elif temp == 'three':
        num = num   '3'
    elif temp == 'four':
        num = num   '4'
    elif temp == 'five':
        num = num   '5'
    elif temp == 'six':
        num = num   '6'
    elif temp == 'seven':
        num = num   '7'
    elif temp == 'eight':
        num = num   '8'
    else:
        num = num   '9'

    # covert string to int
    return int(num)

What I came up with is something like this:

def text_to_digit(s):
    temp=''
    num=''
    d={'one':'1', 'two':'2', 'three':'3', 'four': '4', 'five':'5', 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9', 'zero':'0'}
    for i in s:
        if i == '':
            num = num   d[temp]
            temp=''
        else: 
            temp = temp   i
        
    num=num d[temp]
            
    return int(num)

However, it is not returning anything. I've tried to debug, and it seems the problem is with the if statement in the second function. At first space in the string, instead of doing num=num d[temp], it went on to the else statement and did temp=temp i.

Any ideas why this happened? Thanks in advance!

CodePudding user response:

How about the below ? The idea is to split the input string and do a 'lookup' - join everything and convert to int

LOOKUP = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8',
              'nine': '9', 'zero': '0'}


def text_to_digit(s):
    parts = s.split()
    return int(''.join((LOOKUP[p] for p in parts)))


print(text_to_digit('one two two five'))

output

1225

And an ugly 1 liner :-)

print(int(''.join(LOOKUP[x] for x in 'one two two five'.split())))

CodePudding user response:

Please try this,

def text_to_digit(s):
    number=''
    d={'one':'1', 'two':'2', 'three':'3', 'four': '4', 'five':'5', 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9', 'zero':'0'}
    for i in s.split():        
        number=number d[i]    
            
    return int(number)
print(text_to_digit('one two two five'))
1225
  • Related