From 4a273cfbc7081304fccd5f975b66c5499ec20d48 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 26 Jul 2009 19:05:53 -0400 Subject: [PATCH] started list comprehensions --- comprehensions.html | 47 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/comprehensions.html b/comprehensions.html index 79db3bf..03550bc 100644 --- a/comprehensions.html +++ b/comprehensions.html @@ -194,11 +194,11 @@ body{counter-reset:h1 3}
 >>> a_list = [1, 9, 8, 4]
->>> [elem * 2 for elem in a_list]          
+>>> [elem * 2 for elem in a_list]           
 [2, 18, 16, 8]
->>> a_list                                 
+>>> a_list                                  
 [1, 9, 8, 4]
->>> a_list = [elem * 2 for elem in a_list] 
+>>> a_list = [elem * 2 for elem in a_list]  
 >>> a_list
 [2, 18, 16, 8]
    @@ -207,16 +207,51 @@ body{counter-reset:h1 3}
  1. It is safe to assign the result of a list comprehension to the variable that you’re mapping. Python constructs the new list in memory, and when the list comprehension is complete, it assigns the result to the original variable.
-

FIXME +

You can use any Python expression in a list comprehension, including the functions in the os module for manipulating files and directories.

->>> glob.glob('*.xml')
+>>> import os, glob
+>>> glob.glob('*.xml')                                              
 ['feed-broken.xml', 'feed-ns0.xml', 'feed.xml']
->>> [os.path.abspath(filename) for filename in glob.glob('*.xml')]
+>>> [os.path.abspath(filename) for filename in glob.glob('*.xml')]  
 ['c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-broken.xml',
  'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-ns0.xml',
  'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed.xml']
 
+
    +
  1. FIXME +
  2. +
+ +

List comprehensions can also filter items, producing a result that may be smaller than the original list. + +

+>>> import os, glob
+>>> [f for f in glob.glob('*.py') if os.stat(f).st_size > 6000]  
+['pluraltest6.py',
+ 'romantest10.py',
+ 'romantest6.py',
+ 'romantest7.py',
+ 'romantest8.py',
+ 'romantest9.py']
+
+
    +
  1. To filter a list, you can include an if clause at the end of the list comprehension. The expression after the if keyword will be evaluated for each item in the list. If the expression evaluates to True, the item will be included in the output. This list comprehension looks at the list of all .py files in the current directory, and the if expression filters that list by testing whether the size of each file is greater than 6000 bytes. There are six such files, so the list comprehension returns a list of six filenames. +
+ +

All the examples of list comprehensions so far have featured simple expressions — multiply a number by a constant, call a single function, or simply return the original list item (after filtering). But there’s no limit to how complex a list comprehension can be. + +

+>>> import os, glob
+>>> [(os.stat(f).st_size, os.path.abspath(f)) for f in glob.glob('*.xml')]  
+[(3074, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-broken.xml'),
+ (3386, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-ns0.xml'),
+ (3070, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed.xml')]
+
    +
  1. This list comprehension finds all the .xml files in the current working directory, gets the size of each file, and returns a tuple of the file size and the absolute path of each file. +
+ +

FIXME

 >>> print("\n".join(["{0:>8} {1}".format(humansize.approximate_size(os.stat(f).st_size, False), os.path.abspath(f)) for f in glob.glob('*.py')]))