Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using python to return a list of squared integers
  2. squared = [x**2 for x in lst]
  3.        
  4. def generate_squares(a):
  5.     for x in a:
  6.         yield x**2
  7.  
  8. # this is equivalent to above
  9. b = (x**2 for x in a)
  10.        
  11. squared = lambda li: map(lambda x: x*x, li)
  12.        
  13. >>> def sqr(x):
  14. ...     return x*x
  15. ...
  16. >>> map(sqr,range(1,10))
  17. [1, 4, 9, 16, 25, 36, 49, 64, 81]
  18. >>>
  19.        
  20. >>> map(lambda x: x*x,range(1,10))
  21. [1, 4, 9, 16, 25, 36, 49, 64, 81]
  22.        
  23. result = [x*x for x in range(1,10)]
  24.        
  25. a = [1, 2, 3]
  26. b = [x ** 2 for x in a]
  27.        
  28. for sq in (x*x for x in li):
  29.    # do