[Python-ideas] Map to Many Function

Andrew Barnert abarnert at yahoo.com
Sun Aug 16 05:07:10 CEST 2015


On Aug 15, 2015, at 19:15, Wes Turner <wes.turner at gmail.com> wrote:
> 
> 
> On Aug 15, 2015 9:06 PM, <random832 at fastmail.us> wrote:
> >
> >
> >
> > On Sat, Aug 15, 2015, at 22:02, Wes Turner wrote:
> > > On Aug 15, 2015 8:57 PM, <random832 at fastmail.us> wrote:
> > > >
> > > > On Sat, Aug 15, 2015, at 18:54, Wes Turner wrote:
> > > > > Thanks! Hadn't been aware that there is a flatten() func in stdlib.
> > > >
> > > > You should be aware that this will flatten _any_ list or tuple elements
> > > > inside the elements, and it is gone in python 3.
> > >
> > > So it would then flatten e.g. strings without flinching
> >
> > No, a string isn't a tuple or a list.
> >
> > The point is it will turn (1, 2, [3, (4, 5), 6, [7, 8, [9, 10]]]) into
> > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >
> > So if you have [[(1, 2), (1, 2)], [(3, 4), (3, 4)]] it will become [1,
> > 2, 1, 2, 3, 4, 3, 4] while the mapmany idea you originally discussed,
> > and the solutions other people have given with itertools chain, would
> > give [(1, 2), (1, 2), (3, 4), (3, 4)]
> >
> > Look for yourself, the source code is pretty understandable:
> >
> > def flatten(seq):
> >     l = []
> >     for elt in seq:
> >         t = type(elt)
> >         if t is tuple or t is list:
> >             for elt2 in flatten(elt):
> >                 l.append(elt2)
> >         else:
> >             l.append(elt)
> >     return l
> >
> > You can see it recursively calls flatten on every tuple or list element.
> 
> Got it. So there is no forwardports.flatten in py3k?
> 
Why would you expect a forward port of an undocumented function, especially one that trivial?

Also, given that flatten doesn't do what you want here, and there's also a stdlib function (chain) that does what you want?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150815/86ea8be8/attachment.html>


More information about the Python-ideas mailing list