[Python-ideas] Map to Many Function

Wes Turner wes.turner at gmail.com
Sun Aug 16 04:15:29 CEST 2015


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?

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150815/58942116/attachment.html>


More information about the Python-ideas mailing list