Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Ian Kelly ian.g.kelly at gmail.com
Fri Apr 4 04:13:13 EDT 2014


On Fri, Apr 4, 2014 at 1:52 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> py> from decimal import *
> py> getcontext().prec = 16
> py> x = Decimal("0.7777777777787516")
> py> y = Decimal("0.7777777777787518")
> py> (x + y) / 2
> Decimal('0.7777777777787515')
>
> "Guido, why can't Python do maths???"

Well, you need to work within the system:

>>> (5*x + 5*y) / 10
Decimal('0.7777777777787517')

Actually, I have no idea whether that formula can be relied upon or
the correctness of the above was just luck.



More information about the Python-list mailing list