[Python-ideas] Map to Many Function
Wes Turner
wes.turner at gmail.com
Sun Aug 16 02:47:51 CEST 2015
On Aug 15, 2015 6:52 PM, "Guido van Rossum" <guido at python.org> wrote:
>
> On Sun, Aug 16, 2015 at 1:17 AM, Emile van Sebille <emile at fenx.com> wrote:
>>
>> On 8/15/2015 12:18 PM, Mark Tse wrote:
>>>
>>> Currently, when the function for map() returns a list, the resulting
object
>>> is an iterable of lists:
>>>
>>>>>> list(map(lambda x: [x, x], [1, 2, 3, 4]))
>>>
>>> [[1, 1], [2, 2], [3, 3], [4, 4]]
>>>
>>> However, a function to convert each element to multiple elements,
similar
>>> to flatMap (Java) or SelectMany (C#) does not exist, for doing the
>>> following:
>>
>>
>> In addition to the itertools solutions already posted, there's also a
flatten function that'll do it:
>>
>> Python 2.7.6 (default, Mar 22 2014, 22:59:56)
>> [GCC 4.8.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> from compiler.ast import flatten
>> >>> flatten ((map(lambda x: [x, x], [1, 2, 3, 4])))
>> [1, 1, 2, 2, 3, 3, 4, 4]
>> >>>
>
>
> That function isn't meant for public consumption though.
* notes about flatten in toolz: https://github.com/pytoolz/toolz/issues/176
* flatten in fn.py: https://github.com/kachayev/fn.py#itertools-recipes
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> 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/da1a97db/attachment.html>
More information about the Python-ideas
mailing list