Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use anonymous functions in Python

Shulou Source: shulou.com Published: 2022-06-02 06:50:23 10月03日 Update

This article shows you how to use anonymous functions in Python. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

one。 Define an anonymous function in MATLAB as follows:

F = @ (x1, x2,...) Exper

@ is followed by an independent variable in parentheses, followed by a space (without spaces), and then about x1, x2,. The expression of the.

Here are a few examples.

1.

> > f = @ (x) x.^ 2-1

F =

@ (x) x.^ 2-1

> > x =-1DU 0.01RU 1

> > y = f (x)

> > plot (x, y)

two。

Create a thesaurus

> > words= {}

> > for I = 'axiaoguanghuanghui'

Words = [words, I]

End

% create a library of words corresponding to meaning, and the two of them correspond to each other according to location

> > meaning = {}

Meaning =

{}

> > for I = 1:length (word)

Meaning {I} = I

End

Find out the meaning of the word

> > word ='f'

> > f = @ (s) strcmp (s, word)

> > wordIndex = cellfun (f, words)

> > expalantation = meaning (wordIndex)

Expalantation =

[6]

The previous two for loops are equivalent to setting up two small word databases, and the meanings of words are related by their corresponding positions.

When it comes to the next step, type the word 'fcentury, and then create an anonymous function, f, whose argument is s. The function of this anonymous function is to compare s with the input word, and if the same returns true.

Then it's time for cellfun. The first argument is the function handle, and the second argument is a cell, passing every element in the cell to the function corresponding to the function handle. Then the wordIndex returned is a logical array that is used to retrieve the corresponding element when the position is true.

two。 Anonymous function in Python, (lambda expression try)

F = lambda parameter: parameter expression

Here's a simple example, which is actually similar to that in matlab.

For example: draw x ^ 2-1 image

Import matplotlib.pyplot as plt

Import numpy as np

X = np.arange (- 1,1,0.01)

F = lambda x: Xerox 2-1

Y = f (x)

Plt.plot (XBI y)

Plt.show ()

Python also has something similar to arrayfun and cellfun in MATLAB, that is, the map function, which calculates each element in the iterable sequence as an argument to the function and returns a map object (which is a generator), which can be converted into a list with list.

So the y above can be written as list (map (f, x)).

The above is how to use anonymous functions in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Functions words parameters positions elements two examples content handles meanings skills knowledge spaces arguments expressions input same concise almost concise Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL vpn Docker Shulou Tech Info