To count number of quadruplets with sum = 0

Paul Rubin http
Fri Mar 16 04:08:46 CET 2007

Paul Rubin <> writes:
> print sum([da.get(-(p[0]+q[1]), 0) for p in quads for q in quads])

The above should say:

  print sum(da.get(-(p[0]+q[1]), 0) for p in quads for q in quads)

I had to use a listcomp instead of a genexp for testing, since I'm
still using python 2.3, and I forgot to patch that before pasting to
the newsgroup.  But the listcomp will burn a lot of memory when the
list is large.

I'd be interested in the running time of the above for your large data set.  
Note that it still uses potentially O(n**2) space.

More information about the Python-list mailing list