5 ms·
That's because there's an argument to be made here that the blog poster here is misusing the comprehensions syntax. While what he does works, it can just as ea
by zardeh 10y ago
That's because there's an argument to be made here that the blog poster here is misusing the comprehensions syntax.
While what he does works, it can just as easily be written as
list(itertools.chain(*[[x for x in sublist if x%3 == 0] for sublist in nested_list]))
Which uses a bit of magic to massage the end result, but it is much clearer for the inner portions, and much similar to your 'lispy' style. (`itertools.chain(` is equivalent to a 1-level flatten)
You can do the same thing in the bigger case, and you end up nesting your structures such that you get something like
results = [[[[[[[[[[[[[[[(a, b, g, d, e, z, et, th, i, k, th, mu, n, xi, o, pi, r, s)
for th in range(1, 27) if (2*(th**2) + o**2 + r**2 == 1000) and ((th**2 * z**2 * (s - z) + s**2) == 7225)]
for o in range(1, 27) if (b**3 + z**3 + n**3 + o**9 == 1997)]
for r in range(1, 27)]
for z in range(1, 27)]
for n in range(1, 27) if F(a, k)**2 + F(b, n)**2 + F(d, xi)**2 + F(e, pi)**2 + F(et, mu)**2 + F(i, s)**2 == 6]
for b in range(1, 27)]
for e in range(1, 27)]
for s in range(1, 27)]
for (a, mu) in ((4, 1), (2, 2), (1, 4)) if a*(a+pi) == 4 * g]
for pi in range(1, 27)]
for g in range(1, 27) if (g-1)**2 + d**2 + xi**2 - xi == 600]
for d in range(1, 27)]
for (et, i) in ((xi-7, xi-11), (xi-11,xi-7), (xi+7, xi+11), (xi+11, xi+7)) if 0 < et <= 26 and 0 < i <= 26]
for xi in range(1, 27)]
for k in (3,4)]
Which admittedly isn't better*, but you can build up over time (ie. you can start with [f(k) for k in range(3, 4)], and then provide f(k) in a closure, and repeat, to build this up over time.