[Python-ideas] Map to Many Function
Ryan Gonzalez
rymg19 at gmail.com
Sun Aug 16 01:08:28 CEST 2015
More information about the Python-ideas mailing list
Sun Aug 16 01:08:28 CEST 2015
- Previous message (by thread): [Python-ideas] Map to Many Function
- Next message (by thread): [Python-ideas] Map to Many Function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Wow...I was completely unaware of this! It's worth noting that *this is Python 2-only*. It will give an ImportError with Python 3. :( I really wish the stdlib had something like this, considering that I use it constantly. On Sat, Aug 15, 2015 at 5:17 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 > >>> 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/ > -- Ryan [ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong. http://kirbyfan64.github.io/ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150815/64a7347f/attachment.html>
- Previous message (by thread): [Python-ideas] Map to Many Function
- Next message (by thread): [Python-ideas] Map to Many Function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list