Analysis of list parsing and generating expressions in Python

Source: Internet
Author: User

List Parsing

You can use list resolution when you need to change the list instead of creating a new list. The list parsing expression is:

 
 
  1. [expr for iter_var in iterable]   
  2. [expr for iter_var in iterable if cond_expr]  

First, iterate all the content in iterable. In each iteration, the corresponding content in iterable is put into iter_var, and then the content of iter_var is applied to the expression, finally, a list is generated using the calculated value of the expression.

Syntax 2: A judgment statement is added. Only the content that meets the conditions puts the corresponding content in iterable into iter_var, and then applies the content of the iter_var in the expression, finally, a list is generated using the calculated value of the expression.

Example:

 
 
  1. >>> L= [(x+1,y+1) for x in range(3) for y in range(5)]   
  2. >>> L  
  3. [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)] 
 
 
  1. >>> N=[x+10 for x in range(10) if x>5]  
  2. >>> N  
  3. [16, 17, 18, 19] 
 
 
  1. newlist=[x+5 for x in olderlist if x>10] 

A more complex example:

 
 
  1. <span style="font-size: 14px;">>>> num = [j for i in range(2, 8) for j in range(i*2, 50, i)]  
  2. >>> num  
  3. [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 6,
  4.  9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 8, 12, 16, 20, 24, 28, 32, 36, 40, 
  5. 44, 48, 10, 15, 20, 25, 30, 35, 40, 45, 12, 18, 24, 30, 36, 42, 48, 14, 21, 28, 35, 42, 49]  
  6. </span>  
 
 
  1. >>> words = 'The quick brown fox jumps over the lazy dog'.split()  
  2. >>> words  
  3. ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']  
  4. >>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]  
  5. >>> for i in stuff:  
  6.     print i  
  7.  
  8.       
  9. ['THE', 'the', 3]  
  10. ['QUICK', 'quick', 5]  
  11. ['BROWN', 'brown', 5]  
  12. ['FOX', 'fox', 3]  
  13. ['JUMPS', 'jumps', 5]  
  14. ['OVER', 'over', 4]  
  15. ['THE', 'the', 3]  
  16. ['LAZY', 'lazy', 4]  
  17. ['DOG', 'dog', 3]  

Map () implementation of the above Code:

 
 
  1. >>> stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)  
  2. >>> for i in stuff:  
  3. ...     print i  
  4. ...   
  5. ['THE', 'the', 3]  
  6. ['QUICK', 'quick', 5]  
  7. ['BROWN', 'brown', 5]  
  8. ['FOX', 'fox', 3]  
  9. ['JUMPS', 'jumps', 5]  
  10. ['OVER', 'over', 4]  
  11. ['THE', 'the', 3]  
  12. ['LAZY', 'lazy', 4]  
  13. ['DOG', 'dog', 3] 

Generator expression

Generator expressions are introduced in python2.4. When the sequence is too long and you only need to get one element at a time, you should consider using generator expressions instead of list parsing. The syntax of the generator expression is the same as that of list parsing, except that the generator expression is enclosed by () instead of [], as follows:

 
 
  1. (expr for iter_var in iterable)   
  2. (expr for iter_var in iterable if cond_expr) 

Example:

 
 
  1. >>> L= (i + 1 for i in range(10) if i % 2)  
  2. >>> L  
  3. <generator object <genexpr> at 0xb749a52c>  
  4. >>> L1=[]  
  5. >>> for i in L:  
  6. ...     L1.append(i)  
  7. ...   
  8. >>> L1  
  9. [2, 4, 6, 8, 10] 

The generator expression does not actually create a number list. Instead, it returns a generator. After calculating an entry, the generator "generates" the entry (yield. The generator expression uses lazy evaluation, which is also translated as "latency evaluation". I thought it would be better to translate the call by need Method on demand into inertia ), the value is assigned (evaluated) only during retrieval, so the memory usage is more effective when the list is relatively long. A generator object in python is something like a lazy list. The elements are only evaluated as soon as you iterate over them.

Notes:

1. When you need to execute only one loop, try to use the loop instead of list parsing, which is more intuitive as python advocates.

 
 
  1. for item in sequence:  
  2.     process(item) 

2. When built-in operations or types can be implemented in a more direct way, do not use list resolution.

For example, when copying a list, you can use L1 = list (L) instead:

 
 
  1. L1=[x for x in L] 

3. When the sequence is too long and only one element needs to be obtained each time, use the generator expression.

4. List parsing performance is better than map, refer to http://www.qingliangcn.com/2010/02/list-comprehensions%E4%B8%8Elist-map%E6%80%A7%E8%83%BD%E5%AF%B9%E6%AF%94/
 

Link: http://www.cnblogs.com/moinmoin/archive/2011/03/10/lsit-comprehensions-generators.html

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.