Just touch Python for three days, see the tutorial have a practice "of map and reduce. Write a str2float function, put the string '123.456' converts floating-point number 123.456"
Here is my code:
The from functools import reduce Def str2float (f) : Def to_float (x) : Df={' 1 ', 1, '2', 2, '3', 3 '4', 4, '5, 5,' 6 ', 6, '7', 7 '8', 8, '9, 9,' 0 ': 0} Return df [x] Def c10 (x, y=0) : Return * 10 x + y Def d10 (z, k) : * 0.1 * 0.1 m=z + k Return m D=0 For s in f: If s==': I_=f [: d] F_=f/d + 1: F_='0' + f_] [: : - 1 D=d + 1 Ii=the reduce (c10, map (to_float, i_)) Ff=the reduce (d10, map (to_float, f_)) Return ii + ff Results:
Python 3.7.3 (default, Apr 3, 2019, 05:39:12)
[GCC 8.3.0] on Linux
Type "help", "copyright", "credits" or "license" for more information.
> The from one import str2float
> Str2float (' 101.123 ')
101.123 & lt; - a normal
> Str2float (' 0.123 ')
0.12300000000000001 & lt; - the abnormal
> Str2float (' 1.123 ')
1.123
> Str2float (' 1.00123 ')
1.00123
> Str2float (' 0.00123 ')
0.0012300000000000002 & lt; - the abnormal
>
This is where the wrong?
Also, this code and a better writing?
Thank you very much!
CodePudding user response:
refer to the original poster XHFBX response: just touch Python for three days, see the tutorial has a practice of "use of map and reduce. Write a str2float function, put the string '123.456' converts floating-point number 123.456" Here is my code: The from functools import reduce Def str2float (f) : Def to_float (x) : Df={' 1 ', 1, '2', 2, '3', 3 '4', 4, '5, 5,' 6 ', 6, '7', 7 '8', 8, '9, 9,' 0 ': 0} Return df [x] Def c10 (x, y=0) : Return * 10 x + y Def d10 (z, k) : * 0.1 * 0.1 m=z + k Return m D=0 For s in f: If s==': I_=f [: d] F_=f/d + 1: F_='0' + f_] [: : - 1 D=d + 1 Ii=the reduce (c10, map (to_float, i_)) Ff=the reduce (d10, map (to_float, f_)) Return ii + ff Results: Python 3.7.3 (default, Apr 3, 2019, 05:39:12) [GCC 8.3.0] on Linux Type "help", "copyright", "credits" or "license" for more information. > The from one import str2float > Str2float (' 101.123 ') 101.123 & lt; - a normal > Str2float (' 0.123 ') 0.12300000000000001 & lt; - the abnormal > Str2float (' 1.123 ') 1.123 > Str2float (' 1.00123 ') 1.00123 > Str2float (' 0.00123 ') 0.0012300000000000002 & lt; - the abnormal > This is where the wrong? Also, this code and a better writing? Thank you very much! This is a bug, Python can't eliminate CodePudding user response:
Direct a Decimal is good, CodePudding user response:
There is no need to bother, itself comes with such a python function, num is output decimal The from decimal import * S='1.231' Num=Decimal (s) Print (type (num)) CodePudding user response:
1. Directly using the float function: print (float (' 123.45 ')) 2. Directly using the eval function: print (eval () '123.45') 3. Be sure to practice using the reduce and map can also ah, it feels a bit like its kill chickens: the from functools import reduce Def str2float (f) : S=f.s plit (') S1=list (s [0]) [1] s2=list (s) S2. Reverse () S2. Append (' 0 ') To_digit=lambda x: int (x) C10=lambda x, y: * 10 x + y D10=lambda x, y: * 0.1 x + y Ii=the reduce (c10, map (to_digit, s1)) Ff=the reduce (d10, map (to_digit, s2)) Return ii + ff Print (str2float (' 123.45 ')) CodePudding user response:
The problem is the accuracy of float Single-precision floating-point decimal only six more accuracy CodePudding user response:
The from functools import reduce The from decimal import decimal S=input (' please input string:) Def str2float (f) : Def to_float (x) : Df={' 1 ', 1, '2', 2, '3', 3 '4', 4, '5, 5,' 6 ', 6, '7', 7 '8', 8, '9, 9,' 0 ': 0} Return df [x] Def c10 (x, y=0) : Return * 10 x + y Def d10 (z, k) : Tmp1=Decimal (STR) (z) * Decimal (STR (0.1)) Tmp2=Decimal (STR) (k) * Decimal (STR (0.1)) M=a Decimal (STR (tmp1)) + a Decimal (STR) (tmp2) Return m I_=f.s plit ('. ', 1) [0] F_=f.s plit ('. ', 1) [1] F_='0' + f_] [: : - 1 Ii=the reduce (c10, map (to_float, i_)) Ff=the reduce (d10, map (to_float, f_)) Return ii + ff Print (str2float (s)) CodePudding user response:
The reference 4 floor rrrr336 reply: 1. Directly using the float function: print (float (' 123.45 ')) 2. Directly using the eval function: print (eval () '123.45') 3. Be sure to practice using the reduce and map can also ah, it feels a bit like its kill chickens: the from functools import reduce Def str2float (f) : S=f.s plit (') S1=list (s [0]) [1] s2=list (s) S2. Reverse () S2. Append (' 0 ') To_digit=lambda x: int (x) C10=lambda x, y: * 10 x + y nullnullnullnullnullnull