Python内置了一些非常有趣、有用的函数,如:filter、map、reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车。
1. filter函数的功能相当于过滤器。调用一个布尔函数bool_func来迭代遍历每个seq中的元素;返回一个使bool_seq返回值为true的元素的序列。
>>> N=range(10) >>> print filter(lambda x:x>5,N) [6, 7, 8, 9]
2. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值。
>>> N1=[1,2,3] >>> N2=[6,5,4] >>> map(lambda x,y:x+y,N1,N2) [7, 7, 7] >>> map(lambda x:x+3,N1) [4, 5, 6]
3. reduce函数,func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值。
>>> N=range(1,101) >>> reduce(lambda x,y:x+y,N) 5050
例1:用map和reduce实现5的阶乘相加(5!+4!+3!+2!+1!)
>>>print reduce(lambda x,y:x*y,range(1,6)) >>>print reduce(lambda x,y:x*y,range(1,5)) >>>print reduce(lambda x,y:x*y,range(1,4)) >>>print reduce(lambda x,y:x*y,range(1,3)) >>>print reduce(lambda x,y:x*y,range(1,2)) '''
结果为
120 24 6 2 1 '''
#把上一步的结果变成一个阶乘列表
>>>print map(lambda a:reduce(lambda x,y:x*y,range(1,a+1)),range(1,6)) [1, 2, 6, 24, 120]
#最后把阶乘列表相加,第一题解决
>>>print reduce(lambda m,n:m+n,map(lambda a:reduce(lambda x,y:x*y,range(1,a+1)),range(1,6))) 153
例2:用filter将100~200以内的质数过滤出来
质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数
>>>filter(lambda N:len(filter(lambda M:N%M==0,range(2,int(N**0.5)+1)))==0,range(100,201))

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

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...


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

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools