2013/9/17 Carlos Catucci <carlos.catu...@gmail.com> > > Au contraire, la list comprehension e', come dice il nome stesso, > comprensibile anche a chi non conosca il Python, la lambda e' criptica come > puo' esserlo l'operatore ternario del C per un non iniziato. > > https://en.wikipedia.org/wiki/List_comprehension#History
:D > Io pero' ti chiedevo esempi di pseudocodice (e non di codice Python) dove > useresti appunto map e reduce pittosto che le LC. > > Non e' particolarmente interessante focalizzarsi ne' su map/reduce ne' sulle collezioni in generale. Comunque, un altro esempio e' questo => puoi usare itertools.chain per implementare una flatmap (sicuro che non vuoi il codice Python? allora non leggere :) ) Da un mio scambio privato con un pythonista da queste parti: >>> func = lambda 2 * x >>> func(2) 4 >>> a = [1, 2, 3, 4] >>> b = [2, 3, 4, 5] >>> map(func, a) [2, 4, 6, 8] >>> from itertools import * >>> list(chain(*[a, b])) [1, 2, 3, 4, 2, 3, 4, 5] >>> list(chain([a, b])) [[1, 2, 3, 4], [2, 3, 4, 5]] >>> list(chain(*map(func, a))) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable >>> list(chain(*map(func, [a, b]))) [1, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4, 5, 2, 3, 4, 5] >>> func2 = lambda iter: sum(iter) >>> func2(a) 10 >>> list(chain(*map(func2, [a, b]))) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable >>> func2 = lambda iter: [sum(iter)] >>> func2(a) [10] >>> map(func2, [a, b]) [[10], [14]] >>> list(chain(*map(func2, [a, b]))) [10, 14] >>> list(chain.from_iterable(map(func2, [a, b]))) # this saves you from typing at the expense of typing from_iterable [10, 14] >>> Cosi' facendo se la tua collezione e' gia' flat hai un'eccezione (perche' i singoli elementi non sono iterabili). Qual e' il modo migliore per implementare una flatmap in Python? Ciao, Giuliano -- Piergiuliano Bossi Blog: http://thinkingbox.wordpress.com/ Twitter: http://twitter.com/thinkingbox (English) Twitter: http://twitter.com/scatolapensante (Italiano) Google+: https://plus.google.com/u/0/108187981162465525118
_______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python