From the wrong middle school python (2) ———— string to floating point number

Source: Internet
Author: User

Topic

Write your own functions: use map and reduce to write a str2float function that converts the string ' 123.456 ' to a floating-point number of 123.456:
Source of title--Tribute to Xuefeng Liao

Initial solution to the process
 def str2float(s):     def char2num(s):        return{' 0 ':0,' 1 ':1,' 2 ':2,' 3 ':3,' 4 ':4,' 5 ':5,' 6 ':6,' 7 ':7,' 8 ':8,' 9 ':9}[s]#这事实上是一个字典Index_point=s.find ('. ')ifindex_point==-1: daichu=1    Else: daichu=0.1* * (LEN (s)-1-index_point) s=s[0: index_point]+s[index_point+1:]#这里是除去小数点     fromFunctoolsImportReduce Result1=reduce (Lambdax,y:x*Ten+y,map (Char2num,s))returnResult1*daichu

This uses the Find () function of the string for pattern matching.


This seems to be no problem, but the python3.0 0.1 of the three-time side is:

>>> 0.1**30.0010000000000000002

So that's not going to work, so we're using division.

Use Division instead
 def str2float(s):     def char2num(s):        return{' 0 ':0,' 1 ':1,' 2 ':2,' 3 ':3,' 4 ':4,' 5 ':5,' 6 ':6,' 7 ':7,' 8 ':8,' 9 ':9}[s]#这事实上是一个字典Index_point=s.find ('. ')ifindex_point==-1: daichu=1    Else: daichu=Ten* * (LEN (s)-1-index_point) s=s[0: index_point]+s[index_point+1:]#这里是除去小数点     fromFunctoolsImportReduce Result1=reduce (Lambdax,y:x*Ten+y,map (Char2num,s))returnResult1/daichu

This will give you the right results.

But here we use the Find () function to generate a new string with a slice, which can be used with the split () function

 def str2float(s):     def char2num(s):        return{' 0 ':0,' 1 ':1,' 2 ':2,' 3 ':3,' 4 ':4,' 5 ':5,' 6 ':6,' 7 ':7,' 8 ':8,' 9 ':9}[s]#这事实上是一个字典Strs,index_point=s.split ('. '), Len (S.split ('. ')[1]) daichu=Ten**index_point s=strs[0]+strs[1]#这里是除去小数点     fromFunctoolsImportReduce Result1=reduce (Lambdax,y:x*Ten+y,map (Char2num,s))returnResult1/daichu

The char2num here is in fact completely unnecessary, since there is already a constructor of int (str)

Use the INT function instead
def str2float(s):    strs,index_point=s.split(‘.‘),len(s.split(‘.‘)[1])    daichu=10**index_point    s=strs[0]+strs[1]#这里是除去小数点    fromimport reduce    result1=reduce(lambda x,y:x*10+y,map(int,s))    return result1/daichu

Of course we can also calculate the number of decimal parts, so the code will be shorter. Take shorter time to think and write shorter code

The number of decimal parts is counted backwards.
fromimport reducedef str2float(s):    a = s.split(‘.‘)    return reduce(lambda x, y: x*10+y, map(int, a[0]))           + reduce(lambda x, y: x/10+y, map(int, a[1][::-110

A[1][::-1]) This method is very ingenious. Can be used to reverse the string output. Like what
A[::-1]
That is, the set step is 1 from right to left, the second parameter indicates where to start on the right, and the first parameter represents the first location to be taken.

From the wrong middle school python (2) ———— string to floating point number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.