Usage of Python list Connotation

Source: Internet
Author: User
Tags python list

How to compile the Python list. In fact, there are a lot of problems in use. Let's take a look at the usage. A famous example of how to compile the connotation of a list is to generate a 9-9 multiplication table.

  • Summary of Python Process Control keywords
  • Introduction to two common methods for connecting Python to a database
  • Tips for sharing Python Chinese Characters
  • Summary of common Python application scopes
  • How to convert a Python program to an EXE file

Python List connotation List Comprehensions, also translated as "List Derivation") is one of Python's most powerful syntaxes. It is often used to selectively obtain and compute elements from a collection object, in most cases, you can use a combination of for and if statements to complete the same task, but the code written in the list content is more concise and sometimes difficult to read ).

The general form of the List connotation is as follows. We can write the list connotation in [] into one row, or write it into multiple rows. Generally, multiple rows are easier to read ).

[Expression for item1 in sequence 1... for itemN in sequence N if condition expression]

The expression above is divided into three parts. The leftmost part is the expression that generates each element, and then the for iteration process. On the rightmost side, you can set an if judgment as a filter condition.

A famous example of the meaning of the list is to generate a 9-9 multiplication table:

 
 
  1. s = [(x, y, x*y) for x in range(1, 10) for y in range(1,10) if x>=y] 

The Connotation of the list may be more appropriate in the function programming chapter, because it can be used to uniformly implement the next chapter of high-level functions such as map and filter ). However, I still tend to think of it as a combination of process control statements, and I personally feel that it is a little different from that in C #'s LINQ, of course, it is more powerful, and it can process databases and XML ). The following are two examples: one is implemented using LINQ and the other is implemented using the list connotation of Python.

 
 
  1. C # Use LINQ to find the even number within 10
  2. Var s = from x in Enumerable. Range (0, 10) where x % 2 = 0 select x;
 
 
  1. # Use the list content in Python to simulate the above LINQ statements
  2. 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 the list content to complete more complex programming tasks, in addition, the efficiency is generally higher than the combination of for and if statements, because some list generation and value assignment processes are omitted in the middle ). After Python 2.5, the list connotation is further extended. If a function accepts an iteratable object as a parameter, it can pass a list connotation without brackets, in this way, you do not need to generate the entire list at a time, as long as the iteratable object is passed to the function.

Dynamic Expression

First, assign a question: in C # language, if you need to enter 1 + 2 or a more complex mathematical expression in the text box) and then calculate its value, what would you do?

I am not afraid of everyone's smile. When I use C # To solve this problem, I made an expression parser myself ............, Although only a simple combination of addition, subtraction, multiplication, and division can be calculated, it also takes a lot of effort. Later, I began to use various third-party Parse components and msscript. Now we have Python. It can be very simple to complete this task. You can't believe it as long as you use the built-in eval () function, you can calculate and return the value of any valid expression. For example:

 
 
  1. str = '1+2'2 print eval(str) 

You can also test more complex expressions. Is it a very Powerful function?

In addition to the eval function, Python also provides the exec statement to execute str as a valid Python code. See the following example:

 
 
  1. #exec.py  
  2. exec 'a=100'3 print a 

In addition, the execfile function is used to execute an external py file. After exec. py is saved in the previous example, run the following code to know what is going on:

 
 
  1. execfile(r'c:\exec.py') 

Finally, the default eval (), exec, execfile () code is run in the current namespace, eval (), exec, and execfile () the function can also accept one or two optional dictionary parameters as the global namespace and local namespace for code execution. For details, refer to the Python manual.

Related Article

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.