In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.