[BangPypers] List Comprehensions Vs. map/filter (Was: if not with comparision statement in python)
Dhananjay Nene
dhananjay.nene at gmail.com
Tue Aug 2 06:20:29 CEST 2011
On Mon, Aug 1, 2011 at 8:30 PM, Anand Chitipothu <anandology at 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))))
More information about the BangPypers
mailing list