This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2008年11月17日 15:54 by jmfauth, last changed 2022年04月11日 14:56 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| unnamed | jmfauth, 2008年11月18日 07:35 | |||
| Messages (4) | |||
|---|---|---|---|
| msg75965 - (view) | Author: jmf (jmfauth) | Date: 2008年11月17日 15:54 | |
win XP sp2, Py3.0c2
I had to face an annoying problem when iterating over a map object.
With a range class, this works
>>> r = range(5)
>>> list(r)
[0, 1, 2, 3, 4]
With dict_keys/values/items objects, the following works
>>> d = {1: 'a', 2:'b', 3:'c'}
>>> list(d.keys())
[1, 2, 3]
>>> list(d.values())
['a', 'b', 'c']
>>> list(d.items())
[(1, 'a'), (2, 'b'), (3, 'c')]
But with a map object...
>>> def plus(i):
return i + 1
>>> a = list(range(3))
>>> a
[0, 1, 2]
>>> r = map(plus, a)
>>> r
<map object at 0x01371570>
>>> for e in r: print(e)
1
2
3
>>> list(r)
[]
>>> #empty list!
Bug or feature?
|
|||
| msg75969 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2008年11月17日 16:06 | |
Feature :-) You will get the expected result if you skip the step where you ran the for-loop over r before running list(). Either listing or for-looping will exhaust the iterator. This is how iterators work. |
|||
| msg76001 - (view) | Author: jmf (jmfauth) | Date: 2008年11月18日 07:35 | |
2008年11月17日 Raymond Hettinger <report@bugs.python.org> > > Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment: > > Feature :-) > > You will get the expected result if you skip the step where you ran the > for-loop over r before running list(). Either listing or for-looping > will exhaust the iterator. This is how iterators work. > > ---------- > nosy: +rhettinger > resolution: -> invalid > status: open -> closed > > _______________________________________ > Python tracker <report@bugs.python.org> > <http://bugs.python.org/issue4337> > _______________________________________ > Thanks for the reply and sorry for the noise. Indeed, you are right and for some "strange personal reasons" (bad habits?), I frequently fall in this trap. return i + 1 [1, 2, 3, 4] >>> Regards |
|||
| msg76229 - (view) | Author: Terry J. Reedy (terry.reedy) * (Python committer) | Date: 2008年11月22日 01:25 | |
Dict views and range objects are *iterables* because they are based on reusable information. Map, filter, and similar objects are *iterators* because they are based on iterables that could be once-through iterators. The built-in function entries carefully specific which is which. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:56:41 | admin | set | github: 48587 |
| 2008年11月22日 01:25:34 | terry.reedy | set | nosy:
+ terry.reedy messages: + msg76229 |
| 2008年11月18日 07:35:23 | jmfauth | set | files:
+ unnamed messages: + msg76001 |
| 2008年11月17日 16:06:04 | rhettinger | set | status: open -> closed resolution: not a bug messages: + msg75969 nosy: + rhettinger |
| 2008年11月17日 15:54:35 | jmfauth | create | |