List resolution and build expressions
Imagine a requirement: we already have a list of Lista, LISTB, and now we need to generate a list LISTC so that LISTC is the result of +1 of each element in the Lista, or the result of a and b addition, what should be done.
In this section, we'll learn about this related technology--list parsing and generating expressions.
1. Demand
One of Python's design principles is simplicity, so we often need to create objects quickly, rather than implementing the full details like C. C language Processing, in fact, the value of the operation of each memory object; in Python
We take more advantage of the idea of functional programming-though Python is not a functional language.
2. List resolution principle "[X**2 for X in range (6)]
"0,1,4,9,16,25"
Above is a simple example of list parsing, abstracted out:
[Expr for Iter_var in iterable]
Further can be so abstract: X belongs to set A and performs F (x) on each element x in set a: [F (x) for x in A]
[Expr for Iter_var in iterable if COND_EXPR]
Further: The abstraction of the composite function
>>>f=open ("Text.txt", "R")
>>>len ([Word for line in F for word in line.split ()])
3. Builder expression in list resolution, the newly generated list is all in memory and takes up more space. We now need to implement deferred computing, so we use the generator expression. Syntax and list resolution are basically the same
[Expr for Iter_var in iterable] becomes:
(Expr for Iter_var in iterable)
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.