Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
sjdevnull at yahoo.com
sjdevnull at yahoo.com
Thu Feb 18 18:04:28 EST 2010
On Feb 18, 11:15 am, Steve Howell <showel... at yahoo.com> wrote:
> def print_numbers()
> [1, 2, 3, 4, 5, 6].map { |n|
> [n * n, n * n * n]
> }.reject { |square, cube|
> square == 25 || cube == 64
> }.map { |square, cube|
> cube
> }.each { |n|
> puts n
> }
> end
>
> IMHO there is no reason that I should have to name the content of each
> of those four blocks of code, nor should I have to introduce the
> "lambda" keyword.
You could do it without intermediate names or lambdas in Python as:
def print_numbers():
for i in [ cube for (square, cube) in
[(n*n, n*n*n) for n in [1,2,3,4,5,6]]
if square!=25 and cube!=64 ]:
print i
But frankly, although there's no reason that you _have_ to name the
content at each step, I find it a lot more readable if you do:
def print_numbers():
tuples = [(n*n, n*n*n) for n in (1,2,3,4,5,6)]
filtered = [ cube for (square, cube) in tuples if square!=25 and
cube!=64 ]
for f in filtered:
print f
More information about the Python-list
mailing list