[Python-ideas] Map to Many Function
Emile van Sebille
emile at fenx.com
Sun Aug 16 00:17:32 CEST 2015
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]
>>>
Emile
More information about the Python-ideas
mailing list