Home > Backend Development > Python Tutorial > Python uses reduce and map to convert strings into numbers

Python uses reduce and map to convert strings into numbers

黄舟
Release: 2017-01-18 15:52:15
Original
1473 people have browsed it

Python Programming Language

Python is an object-oriented, interpreted computer programming language invented by Guido van Rossum at the end of 1989. The first public release was released in 1991. Python syntax is concise and clear, with rich and powerful class libraries. It is often nicknamed the glue language, which can easily connect various modules made in other languages ​​​​(especially C/C++) together.


#When I was reviewing higher-order functions recently, there was a question that I couldn't solve after thinking about it for a long time. So I searched for information in the morning and looked at other people's solutions. I found that thinking is really important when learning programming. The following article will introduce to you the ideas and methods of converting strings into numbers using reduce and map in python. Friends in need can refer to it. Let's take a look together.

Introduction to reduce and map in python

map(func,seq1[,seq2...]): Apply the function func to each element of the given sequence, and use a A list to provide the return value; if func is None, func behaves as an identity function, returning a list of n tuples containing the set of elements in each sequence.

reduce(func,seq[,init]): func is a binary function, apply func to the elements of the seq sequence, each time it carries a pair (the previous result and the elements of the next sequence), continuously The existing result and the next value are applied to the subsequent results obtained, finally reducing our sequence to a single return value: if the initial value init is given, the first comparison will be between init and the first sequence elements instead of the first two elements of the sequence.

This article mainly introduces how Python uses reduce and map to convert strings into numbers. Without further ado, let’s take a look at the detailed implementation method.

Exercises:

Use map and reduce to write a str2float function to convert the string '123.456' into a floating point number 123.456

Solution and idea explanation:

from functools import reduce
 
def str2float(s):
 s = s.split('.') #以小数点为分隔符,把字符串分为两部分
 
 def f1(x,y): #函数1,小数点之前的数用这个函数处理
  return x * 10 + y
 
 def f2(x,y): #函数2,小数点之后的数用这个函数处理
  return x / 10 + y
 
 def str2num(str): #函数3,用于把字符串'123'逐个变为数字
  return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[str]
 
 return reduce(f1,map(str2num,s[0])) + reduce(f2,list(map(str2num,s[1]))[::-1])/10
 
 #最后一部是这个解法的精髓
 #小数点前的数'123',用 x * 10 + y 正常求和就能得出123,小数点之后的数'456'要怎样才能得出0.456呢?
 #首先把字符串'456'用list(map(str2num,s[1]))转成一个列表[4,5,6]
 #然后用[::-1]切片的方式从后往前取,列表变为[6,5,4]
 #然后把[6,5,4]利用reduce函数放到f2函数中计算,( 6 / 10 + 5) / 10 + 4 = 4.56,得出结果4.56
 #再除以一个10,得出0.456,到此成功把字符串'456'变成了浮点数0.456
 #把前后结果加起来,就得到了最终解,成功把字符串'123.456'变成了浮点数123.456
Copy after login

The above is the content of Python's method of converting strings into numbers using reduce and map. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template