[Baypiggies] Baypiggies snippets

Chad Netzer chad.netzer at gmail.com
Thu Mar 22 19:44:47 CET 2007


On 3/21/07, Doug Landauer <zia at cruzio.com> wrote:
> On Mar 21, 2007, at 8:22 PM, Keith Dart ♂ wrote:
> > It's so general, I think it should be a built-in function and not in a
> > GUI toolkit.
>
> For what it's worth, Ruby's Array class has just such a "flatten"
> method.

Actually, Python's numpy extension provides this as well, although
these arrays have fixed dimensionality, rather than the arbitrary
depths that one may encounter with sequences of sequences of
sequences...

And, in fact, if you have a known depth, you can construct a simple,
restricted "flatten" with the itertools module.  So, for example, to
make a list of 2-D or 3-D coordinates into a flat list:

import itertools

a = [[1,2], [3,4], [5,6]]   # list of 2-D coords, works for n-D coords as well
b = itertools.chain(*a)

list(b) == [1, 2, 3, 4, 5, 6]

Chad


More information about the Baypiggies mailing list