The way Python uses reduce and map to convert strings to numbers _python

Source: Internet
Author: User
Tags in python

Introduction to reduce and map in Python

map(func,seq1[,seq2...]) : func function to each element of a given sequence, using a list to provide the return value, or, if Func is an identity function for None,func, returns a list of n tuples containing the set of elements in each sequence.

reduce(func,seq[,init]) : Func is a two-dollar function, the elements that act on the seq sequence, each carrying a pair (the previous result and the elements of the next sequence), continue to effect the existing results and the next value on the resulting subsequent result, and finally reduce our sequence to a single return value: If the initial value init is given, the Func The first comparison will be init and the first sequence element rather than the first two elements of the sequence.

This article focuses on Python's use of reduce and map to convert strings to numbers, not to mention the following, to see the detailed implementation method.

Exercises:

Use map and reduce to write a str2float function that converts the string ' 123.456 ' to floating-point number 123.456

Solution and Idea Explanation:

From Functools import reduce
 
def str2float (s):
 s = s.split ('. ') #以小数点为分隔符, divide the string into two-part
 
 def F1 (x,y): #函数1, The number before the decimal point is processed with this function return
  x * + y
 
 def f2 (x,y): #函数2, the number after the decimal point is processed with this function return
  X/10 + y
 
 def str2num (str): #函数3 , used to change the string ' 123 ' to digital return
  {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[str]
 
 re Turn reduce (F1,map (str2num,s[0)) + Reduce (f2,list (map (str2num,s[1))) [: -1])/10
 
 #最后一部是这个解法的精髓
 # The number before the decimal point ' 123 ', with x * + y normal sum can be reached 123, the number after the decimal point of ' 456 ' How to come to 0.456?
 #首先把字符串 ' 456 ' with list (map (str2num,s[1)) is converted into a listing [4,5,6]
 #然后用 [::-1] slices are taken from the back, the list becomes [6,5,4]
 #然后把 [6,5,4] Using the reduce function to calculate in the F2 function, (6/10 + 5)/10 + 4 = 4.56, the result is 4.56
 #再除以一个10, resulting in 0.456, which succeeded in turning the string ' 456 ' into a floating-point 0.456
 #把前后结果加起来, We get the final solution, and we successfully turn the string ' 123.456 ' into a floating-point number 123.456.

Summarize

The above is the entire content of this article, I hope that the content of this article for everyone to learn or use Python can bring some help, if you have questions you can message exchange.

Related Article

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.