On Mon, Aug 1, 2011 at 8:30 PM, Anand Chitipothu <anandol...@gmail.com> wrote:
>> I knew there was a way to better implement flatmap - its a combination
>> of itertools.chain.from_iterable and map. Here's a much cleaner code
>>
>> from itertools import chain
>>
>> print filter(lambda (x,y,z) : x*x + y*y == z*z,
>>    chain.from_iterable(map(
>>        lambda x: chain.from_iterable(map(
>>            lambda y: chain.from_iterable(map(
>>                lambda z: [[x,y,z]],
>>                range(y,100))),
>>            range(x,100))),
>>        range(1,50))))
>>
>
> Impressive. But having to return [[x, y, z]] instead of [x, y,z] is a
> compromise.

No longer. It was there to compensate for an extra chain.from_iterable
(which is not required for the innermost map)

print filter(lambda (x,y,z) : x*x + y*y == z*z,
    chain.from_iterable(map(
        lambda x: chain.from_iterable(map(
            lambda y: map(
                lambda z: [x,y,z],
                range(y,100)),
            range(x,100))),
        range(1,50))))
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to