Newbie: list comprehension troubles..
Ben Finney
ben+python at benfinney.id.au
Sun Aug 23 20:09:29 EDT 2009
Chris Rebert <clp2 at rebertia.com> writes:
> tlist = [pair for pair in ((obj, obj.intersect(ray)) for obj in
> self.objs) if pair[1] is not None]
>
> Should it be done? Probably not. [Compared to a ‘for’ suite with an
> ‘if’ suite, it's] less readable and less efficient.
I disagree on the “less efficient”, unless you've measured it. The
Python compiler and machine make list comprehensions and generator
expressions turn into quite efficient code.
I also disagree on “less readable”, if you show the structure and choose
meaningful names (I can only guess at the meaning from the OP's code)::
tribbles = [
(obj, tribble) for (obj, tribble) in (
(obj, obj.intersect(ray))
for obj in self.objs)
if tribble is not None]
--
\ “I must say that I find television very educational. The minute |
`\ somebody turns it on, I go to the library and read a book.” |
_o__) —Groucho Marx |
Ben Finney
More information about the Python-list
mailing list