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

The actual use of Python list connotations

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the actual use of the connotation of the Python list". In the daily operation, I believe that many people have doubts about the actual use of the connotation of the Python list. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the actual use of the connotation of the Python list. Next, please follow the editor to study!

Python list connotations (List Comprehensions, also translated as "list deduction") is one of the powerful grammars of Python***, which is often used to selectively retrieve and calculate elements from collection objects. Although in most cases you can use statements such as for and if to accomplish the same task, the code written by list connotations is more concise (of course, sometimes it may not be easy to read).

The general form of the list connotation is as follows, we can write the list connotation in [] as one line, or as multiple lines (generally speaking, multiple lines are easier to read).

[expression for item1 in sequence 1... For itemN in sequence N if conditional expression]

The above expression is divided into three parts, the leftmost is to generate the expression of each element, and then the for iteration process, the rightmost can set an if judgment as a filter condition.

A famous example of the connotation of a list is the generation of a ninety-nine multiplication table:

S = [(x, y, x, y) for x in range (1m 10) for y in range (1m 10) if x > = y]

The list connotation may be more appropriate in the functional programming chapter, as it uniformly implements higher-order functions such as map and filter (described in the next chapter). However, I still tend to think of it as a combined flow control statement, and I personally feel that it is somewhat similar to LINQ in C # (of course LINQ is more powerful and can handle databases and XML). Here are two examples, one implemented in LINQ and the other in the list connotation of Python.

In C #, use LINQ to find out the even number var s = from x in Enumerable.Range (0,10) where x% 2 = = 0 select x politics Python using list connotations to simulate the above LINQ statement s = [x for x in range (0,10) if x% 2 = = 0]

Of course, the above example is very simple, in fact, we can use list connotations to complete more complex programming tasks, and generally more efficient than using for, if, etc. (because some list generation and assignment processes are omitted). After Python 2.5, the list connotation has been further extended, and if a function accepts an iterable object as a parameter, it can be passed a list connotation without parentheses, so that it does not need to generate the entire list at once, just pass the iterable object to the function.

Dynamic expression

Let's start with a thought question: in the C # language, what would you do if you needed to type 1: 2 (or a more complex mathematical expression) into the text box and calculate its value?

I am not afraid of everyone laughing. When I used C # to solve this problem, I made an expression parser myself. Although I can only calculate the simple combination of addition, subtraction, multiplication and division, it really cost me a lot of effort. It was only later that we began to use a variety of third-party Parse components, msscript, and so on. Now that we have Python, this task is so simple that you can't believe it: just use the built-in eval () function to evaluate and return the value of any valid expression. For example:

Str ='1x 2 print eval (str)

You can also experiment with more complex expressions, isn't it a very Powerful feature?

In addition to the eval function, Python also provides an exec statement to execute the string str as valid Python code, as shown in the following example:

# exec.py exec'a 100 million 3 print a

There is also the execfile function, which executes an external py file. After the previous example is saved as exec.py, run the following code to see what's going on:

Execfile (rascc:\ exec.py')

* Note: by default, the codes run by eval () and exec,execfile () are located in the current namespace. The eval (), exec, and execfile () functions can also accept one or two optional dictionary parameters as the global and local namespaces for code execution. For more information, please refer to the Python manual.

At this point, the study of "the actual use of the connotation of Python list" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 259

*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