Remove the char string in the string and then use the dictionary to make a key match

I want to write a function to convert a string to float using a map and shortening. (there is no int ()). Here is my code:

from functools import reduce

def str2float(s):
    def char2number(s):
        s = s.replace('.', '')
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 

    def find_digt(s):
        return (len(s) - s.find('.') - 1) if ',' in s else 0

    return reduce(lambda x, y: (10 * x + y), (map(char2number, s))) / pow(10, find_digt(s))


print(str2float('1456.124'))

So, after that I get the error message:

return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[sn]  # [s] is the [key]
KeyError: ''

This means no in the dict. I did some tests like:

s = '1234.456'
s = s.replace(',', '')
print('' in s) #True

So the question is now, once

s = s.replace('.', '')

the. replace with '' he did not clear '.' in line.

I wonder what is going on here. that this is the right way to clear the char in a string since it is immutable.

+4
source share
3 answers

, map , . char2number. .

:

from functools import reduce

def str2float(s):
    pos_decimal_point = (len(s) - s.find('.') - 1) if '.' in s else 0
    s = s.replace('.', '')

    def char2number(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 

    return reduce(lambda x, y: (10 * x + y), 
                  (map(char2number, s))) / pow(10, pos_decimal_point)


print(str2float('1456.124'))
+1

:

def char2number(s):
    print ("ENTER", s)
    s = s.replace('.', '')
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

:

ENTER 1
ENTER 4
ENTER 5
ENTER 6
ENTER .
Traceback (most recent call last):
  File "so.py", line 15, in <module>
    print(str2float('1456.124'))
  File "so.py", line 12, in str2float
    return reduce(lambda x, y: (10 * x + y), (map(char2number, s))) / pow(10, find_digt(s))
  File "so.py", line 7, in char2number
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 
KeyError: ''

"1234.456" char2number. , , .

, . , , ​​ . map .

, , : , , replace . , .

+1

You have the right idea, just replace .in sbefore moving on to your function map:

map(char2number, s.replace('.',''))

Note. There's a typo find_digt(), it should read: if '.' in s....
If you also want to process commas, add extra ones replaceto knock them out:

stripped_s = s.replace('.','').replace(',','')
return reduce(lambda x, y: (10 * x + y), (map(char2number, stripped_s))) / pow(10, find_digt(s))

print(str2float('1,456.124'))
1456.124

Full code:

def str2float(s):
    def char2number(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]  

    def find_digt(s):
        return (len(s) - s.find('.') - 1) if '.' in s else 0

    stripped_s = s.replace('.','').replace(',','')
    return reduce(lambda x, y: (10 * x + y), (map(char2number, stripped_s))) / pow(10, find_digt(s))

    print(str2float('1,456.124'))
    1456.124
+1
source

Source: https://habr.com/ru/post/1677311/


All Articles