The big topic with old Ziko python small function (1) _python

Source: Internet
Author: User
Tags iterable in python

The opening point is to mention a big topic: programming paradigm. What is a programming paradigm? To quote the Wikipedia explanation:

Copy Code code as follows:

Programming Paradigm (English: Programming Paradigm), (fan is exemplary, paradigm, mode, method), is a kind of typical programming style, refers to engaged in software engineering a typical style (can control methodology). Such as: Functional programming, program programming, object-oriented programming, instruction programming and so on for different programming paradigm.

The programming paradigm provides (at the same time) a programmer's view of program execution. For example, in object-oriented programming, programmers think of a program as a series of interacting objects, while in functional programming a program is considered a serial of stateless function computations.

Just as different groups in software engineering advocate different "methodologies", different programming languages also advocate different "programming paradigms". Some languages are designed specifically for a particular paradigm (such as Smalltalk and Java support for object-oriented programming, while Haskell and scheme support functional programming), while others support a variety of paradigms (such as Ruby, Common Lisp, Python and Oz).

The relationship between programming paradigms and programming languages can be complex, as a programming language can support multiple paradigms. For example, C + + design supports procedural programming, object-oriented programming, and generic programming. However, designers and programmers have to consider how to use these paradigm elements to build a program. A person can write a fully procedural program in C + +, another person can write a pure object-oriented program in C + +, and even someone can write the two types of mixed-type procedures.

Whether reader is a beginner or an old dough-stick, it is recommended to read the above paragraph carefully, regardless of understanding or do not understand, always a little feel.

This article is recommended here from the Web: the main programming paradigm

A lot of programming paradigm, today this talk about what? Today we're going to introduce a few small functions in Python, which are all from functional programming, which are:

filter, map, reduce, lambda, yield

With them, the biggest advantage is that the program is more concise, without them, the program can be implemented in other ways, just a little trouble. So, you can use it.

Lambda

A lambda function, a function that solves problems with just one line, sounds tempting. Look at the following example:

Copy Code code as follows:

>>> def Add (x): #定义一个函数, increase the input variable by 3, and then return the value after the increment
... x +=3
... return x
...
>>> numbers = Range (10)
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #有这样一个list, want to increase each number by 3, and then output to a new list

>>> new_numbers = []
>>> for I in numbers:
New_numbers.append (Add (i)) #调用add () function, and append to list
...
>>> new_numbers
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

In this example, add () is just an intermediate operation. Of course, the above example can be implemented in a different way. Like what:

Copy Code code as follows:

>>> new_numbers = [i+3 for i in numbers]
>>> new_numbers
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

First of all, this list parsing is very, very good.

But we have to replace add (x) with the function of lambda, and if reader is as paranoid as I am, I can:

Copy Code code as follows:

>>> Lam = Lambda x:x+3
>>> n2 = []
>>> for I in numbers:
... n2.append (Lam (i))
...
>>> N2
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

The lam here is equivalent to add (x), please reader, this line of Lambda x:x+3 Complete Add (x) three lines (or two lines?). ), especially the last return value. You can also write examples such as:

Copy Code code as follows:

>>> g = lambda x,y:x+y #x +y, and returns the result
>>> g (3,4)
7
>>> (Lambda x:x**2) (4) #返回4的平方
16

Using the example above, summarize the use of lambda functions:
• Follow the variables directly behind the lambda
• The variable is followed by a colon
• After the colon is an expression, the expression evaluates to the return value of this function

In order to be concise, it is necessary to represent a formula:

Copy Code code as follows:

Lambda arg1, arg2, ... argn:expression using arguments

To specifically remind reader: Although a lambda function can receive any number of arguments (including optional arguments) and returns the value of a single expression, a lambda function cannot contain a command and cannot contain more than one expression. Don't try to cram too much into a lambda function; If you need something more complex, you should define a normal function and how long you want it to be.

As far as lambda is concerned, it does not give the program a performance boost, it brings the simplicity of the code. For example, to print a list, which in turn is a number of 1 times, two times, three Times Square, four times Square. You can do this with a lambda:

Copy Code code as follows:

>>> lamb = [Lambda x:x,lambda x:x**2,lambda X:x**3,lambda x:x**4]
>>> for I in Lamb:
... print I (3),
...
3 9 27 81

Lambda is a single line of functions that you can choose to use in programming practice. According to my experience, try to use less, because it may be more to reduce the definition of a single function.

Map

Let's take a look at an example, or the first example of a lambda, which can be implemented with a map:

Copy Code code as follows:

>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #把列表中每一项都加3

The >>> map (add,numbers) #add (x) is the function described above, but only the function name is referenced here
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

>>> map (lambda x:x+3,numbers) #用lambda当然可以啦
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Map () is a built-in function of Python, its basic style is: Map (func, seq), Func is a function, SEQ is a sequence object. At execution time, each element in the sequence object is taken out in order from left to right, and is stuffed into the Func function, and the return value of the Func is saved in a list in turn.

In the application, the map can be implemented, but also can be implemented in other ways. Like what:

Copy Code code as follows:

>>> items = [1,2,3,4,5]
>>> squared = []
>>> for I in items:
... squared.append (i**2)
...
>>> Squared
[1, 4, 9, 16, 25]

>>> def sqr (x): Return x**2
...
>>> Map (Sqr,items)
[1, 4, 9, 16, 25]

>>> map (Lambda X:x**2,items)
[1, 4, 9, 16, 25]

>>> [x**2 for x in items] #这个我最喜欢了, usually fast enough and readable
[1, 4, 9, 16, 25]

All roads lead to Rome, the above methods, in programming, their own according to the need to choose.

On the basis of the above perceptual knowledge, you can understand more about the official description of map ().

Copy Code code as follows:

Map (function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and are applied to the Terables in parallel. If one iterable is shorter than another it are assumed to being extended with None items. The If function is None, the identity function is assumed; If there are multiple arguments, map () returns a list consisting of tuples containing the corresponding items from all ITE Rables (a kind of transpose operation). The iterable arguments may is a sequence or any iterable object; The result is always a list.

Understand the main points:
• For each element in the Iterable, the method (function) that applies the function in turn (this is essentially a for loop).
• Return all results to a list.
• If there are many parameters, the function is executed in parallel with the parameters.

For example:

Copy Code code as follows:

>>> lst1 = [1,2,3,4,5]
>>> lst2 = [6,7,8,9,0]
>>> map (Lambda x,y:x+y, Lst1,lst2) #将两个列表中的对应项加起来, and returns a list of results
[7, 9, 11, 13, 5]

Please reader note that if the above example is written with a for loop, it is not very difficult, if extended, the following example is rewritten with for, be careful:

Copy Code code as follows:

>>> lst1 = [1,2,3,4,5]
>>> lst2 = [6,7,8,9,0]
>>> lst3 = [7,8,9,2,1]
>>> map (Lambda x,y,z:x+y+z, LST1,LST2,LST3)
[14, 17, 20, 15, 6]

This shows the simplicity and elegance of the map.

Notice: Next explain reduce and filter

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.