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
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)!

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),