Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the divine algorithm in Python refer to?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about what the God-like algorithm in Python refers to. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

one。 List derivation

When it comes to the simplest and most amazing code in python, list derivation should be number one.

This is a very concise way of writing, and you can make another according to one list. This expression is called list comprehension (list derivation)

Example 1, use a list to generate a new list

Example 2, you can even filter some elements in the list, such as:

Example 3: if you need to loop through the contents of a sequence, you can also add a function to combine it.

After reading the usage of the list derivation, do you think it is very convenient to shine in front of your eyes?

II. With usage

Generally speaking, when we deal with files, we first open-> then process-> and then close. It's troublesome, and we need to protect try/finally from exceptions, and most of the time we focus on how to deal with files so that we forget to close files. Python has a very concise method:

Normal open and close file processing:

With the with statement, it is very simple to use, a bit like English. Using the with statement can guarantee that the file will be closed automatically after the write operation is completed.

In fact, there are many scenarios for the use of with, in addition to closing the processing of files, the handling of locks is also often used in the use of multithreading. Future articles will talk about the use of multithreading and multi-processes in python.

The use of with embodies a quintessence of python: leave some tedious transactions to the language itself, and developers only need to put focus on the logic of dealing with problems.

Third, anonymous function lambda

Python has a lazy-only function called an anonymous function (that is, a function without a name). When we pass in a function, sometimes it is more convenient to pass in an anonymous function without explicitly defining it.

Lambda (the name is actually borrowed from LISP, another language that hackers like very much), the general form of lambda is the keyword lambda followed by one or more parameters, followed by a colon, followed by an expression:

Lambda arg1,agr2,...agrN:express using arguments

Taking the map () function as an example, to calculate the square of each element in a list, you can pass in the anonymous function directly:

> > map (lambda x: X * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]) [1, 4, 9, 16, 25, 36, 49, 64, 81]

By comparison, we can see that the anonymous function lambda x: X * x is actually:

Def f (x): return x * x

The benefits of using anonymous functions are obvious:

On the one hand, it can avoid the trouble of naming (because high-quality code has certain requirements for the naming of functions)

And you don't have to worry about function name conflicts.

In addition, an anonymous function is also a function object, and you can assign an anonymous function to a variable, and then use the variable to call the function:

4. Generator

Generator is a difficult concept in python and one of the two powerful features introduced in Python (guess what the other feature is, by the way, decorator).

Today I'll take a look at a simple example of it, an implementation of the Fibonacci sequence:

The generator's function is used:

To see if the code of the second method is much simpler, this is the charm of the yield keyword.

If a function definition contains the yield keyword, then the function is no longer an ordinary function, but a generator function, print it.

Print (fac2 (10)) >

The execution process of a generator function is very different from that of a normal function:

The function is executed sequentially and returns when it encounters the declare statement or the last line of the function statement.

The function that becomes the generator will only be run when the corresponding iteration operation occurs. It is usually used with for (also with sum (), list ()).

When executed each time next () is called, the yielding statement returns, and execution continues from the last returned yield statement when executed again.

All right, the above are very magical codes in python. I don't know if you love python after reading it. In fact, simplicity and efficiency is synonymous with python.

After reading the above, do you have any further understanding of what the divine algorithm in Python refers to? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report