Promising Power: functools and itertools of Python

I worked through functools and itertools sections of the Computational Statistics in Python tutorial, and I found these promisingly powerfuul for data modeling and functional programming:


 # The functools module  
 """The most useful function in the functools module is partial, 
 which allows you to create a new function from an old one with 
 some arguments “filled-in”."""  
   
 from functools import partial  
 def power_function(power, num):  
   """power of num."""  
   return num**power  
   
 square = partial(power_function, 2)  
 cube = partial(power_function, 3)  
 quad = partial(power_function, 4)  
   
   
 # The itertools module  
 """This provides many essential functions for working with iterators.   
 The permuations and combinations generators may be particularly useful   
 for simulations, and the groupby gnerator is useful for data analyiss."""  
   
 from itertools import cycle, groupby, islice, permutations, combinations  
   
 simualtedDataSeries = list(islice(cycle('abcd'), 0, 20))  
   
 simulatedSeries = list(islice(cycle('acgt'), 0, 4))  
 [p for p in permutations(simulatedSeries)]  
   
 text = [line for line in open('requirements.txt')]  
 pairs = [(k, g) for k, g in groupby(text, key=len)]  

Comments

Popular posts from this blog

Charting Correlation Matrices in R

Cultural Dimensions and Coffee Consumption

Developers in New York City by Zip Code