Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107604 > unrolled thread
| Started by | oyster <lepto.python@gmail.com> |
|---|---|
| First post | 2016-04-25 22:13 +0800 |
| Last post | 2016-04-25 17:42 +0300 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
what is the difference between one-line-operation and 2-line-operation oyster <lepto.python@gmail.com> - 2016-04-25 22:13 +0800
Re: what is the difference between one-line-operation and 2-line-operation Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 17:42 +0300
| From | oyster <lepto.python@gmail.com> |
|---|---|
| Date | 2016-04-25 22:13 +0800 |
| Subject | what is the difference between one-line-operation and 2-line-operation |
| Message-ID | <mailman.78.1461593638.32212.python-list@python.org> |
for a simple code
[code]
vexList = [1, 2, 3]
print('vexList', list(vexList))
vexList=map(lambda e: e+1, vexList)
print('vexList', list(vexList))
vexList = list(vexList)
print('vexList', list(vexList))
vexList=map(lambda e: e*2,vexList)
print('vexList', list(vexList))
[/code]
py27 says
[quote]
('vexList', [1, 2, 3])
('vexList', [2, 3, 4])
('vexList', [2, 3, 4])
('vexList', [4, 6, 8])
[/quote]
but py34 says
[quote]
vexList [1, 2, 3]
vexList [2, 3, 4]
vexList []
vexList []
[/quote]
if I change the above code in to one line
[code]
vexList = [1, 2, 3]
print('vexList', list(vexList))
vexList=list(map(lambda e: e+1, vexList))
print('vexList', list(vexList))
vexList=map(lambda e: e*2,vexList)
print('vexList', list(vexList))
[/code]
then py27 and py34 get same verList
[quote]
('vexList', [1, 2, 3])
('vexList', [2, 3, 4])
('vexList', [4, 6, 8])
[/quote]
I found 'filter' function behaves likely
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?
Thanks
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-04-25 17:42 +0300 |
| Message-ID | <lf5d1pdvjfo.fsf@ling.helsinki.fi> |
| In reply to | #107604 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web