what is the difference between one-line-operation and 2-line-operation

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Apr 25 10:42:35 EDT 2016


oyster writes:

- -

> I found
> type(map(lambda e: e, vexList))  is <type 'list'> in py2
> type(map(lambda e: e, vexList)) is <class 'map'> in py3
>
> so, what produces this difference between py2 and py3 in nature? is
> there more examples? where can I find the text abiut his difference?

Yes, there are more ways obtain objects that behave like the map and
filter objects on Python 3. One similar to map and filter is enumerate.

Try iter('foo'), iter((1,2,4)), ..., iter(range(3)). This is used
implicitly by certain Python constructions. Note that range objects
themselves are different.

Open a file for reading: open('example.txt'). You get lines out.

A generator expression: ( "{}".format(x) for x in (1,2,3) ), where the
outer parentheses are for grouping and not always needed.

Define and call a generator function:

def foo(o):
   yield '('
   yield o
   yield ')'

x = foo(31)

'(-31-)' == '-'.join(foo('31'))

You can ask for the next element from any of these, or collect their
remaining elements in a list or tuple, or otherwise iterate or map over
them. They will be consumed when you do so, otherwise they just wait.

They can be huge, even infinite. Best not to collect their contents in a
data structure if they are huge. They trade space for time.



More information about the Python-list mailing list