[Python-ideas] Accessing the result of comprehension's expression from the conditional
8mayday at gmail.com
Sun Jun 21 10:54:00 CEST 2009
What about where/let expression?
[(y, y) for x in some_list if y < 0 where y = f(x)]
On Sun, Jun 21, 2009 at 10:36 AM, Lie Ryan<lie.1296 at gmail.com> wrote:
> Ben Finney wrote:
>> Lie Ryan <lie.1296 at gmail.com> writes:
>>> Steven D'Aprano wrote:
>>>> The only case it doesn't cover is where the second filter depends on
>>>> the value of x, and even that can be covered with a *tiny* bit more
>>>> gen = ((x, 3*x**2-5*x+4) for x in seq if x % 3 != 2)
>>>> result = [y for y in gen if -y < y < y]
>>>> It requires no new syntax, no changes to the behaviour of list
>>>> comps, no new meaning on "as", it's understandable and readable.
>> I think it would be more readable without index references, but instead
>> using tuple unpacking::
>> gen = ((x, 3*x**2-5*x+4) for x in seq if x % 3 != 2)
>> result = [b for (a, b) in gen if -a < b < a]
>> It can even be done as a single expression without (IMO) significantly
>> affecting readability::
>> result = [
>> b for (a, b) in
>> ((x, 3*x**2-5*x+4) for x in seq if x % 3 != 2)
>> if -a < b < a]
> IMHO, when a comprehension requires more than a single line, it should
> turn into explicit loop.
> Python-ideas mailing list
> Python-ideas at python.org
С уважением, Андрей Попп.
+7 911 740 24 91
More information about the Python-ideas