[Python-ideas] Map to Many Function

Wes Turner wes.turner at gmail.com
Sun Aug 16 00:54:28 CEST 2015


On Aug 15, 2015 5:18 PM, "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

Thanks! Hadn't been aware that there is a flatten() func in stdlib.

> >>> flatten ((map(lambda x: [x, x], [1, 2, 3, 4])))
>
> [1, 1, 2, 2, 3, 3, 4, 4]
> >>>
>
> Emile
>
>
>
>
> _______________________________________________
> 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/7e3d7a59/attachment.html>


More information about the Python-ideas mailing list