Congruence of 'map' and 'for' broken in 2.1??

nanotech at europa.com nanotech at europa.com
Fri Apr 27 16:19:44 EDT 2001


All:

OK, now that my subject line hooked you :) I am trying to determine
why I can not use fh.xreadlines() in map:

Say I want to sum the integers in a file where there exists one int
per line:

  >>> fh=open(filename)
  >>> sum=0
  >>> for line in fh.xreadlines():
          sum += int(line)
  >>> print sum
  >>> fh.close()

Right? Now the Python Reference Manual explictly states:
  http://www.python.org/doc/2.1/ref/for.html

  The expression list is evaluated once; it should yield a sequence.

Now by this, the for ought to break, but I can live with the fudging
going on here

  ....BUT....

Say I want to do the same thing in the nice simple example given in
the documentation concerning reduce:

  >>> fh=open(filename)
  >>> print reduce(lambda x, y: x+y, map(int,fh.xreadlines()))
  Traceback (most recent call last):
    File "<pyshell#91>", line 1, in ?
      print reduce(lambda x, y: x+y, map(int,fh.xreadlines()))
  TypeError: argument 2 to map() must be a sequence object


Ack!! Why will map not fudge? What am I missing?? Is it pilot error??

Thanks!! Quentin

DETAIL:

  >>> fh=open(filename)
  >>> sum=0
  >>> for line in fh.xreadlines():
          sum += int(line)


  >>> print sum
  55
  >>> fh.close()
  >>> fh=open(filename)
  >>> print reduce(lambda x, y: x+y, map(int,fh.xreadlines()))
  Traceback (most recent call last):
    File "<pyshell#91>", line 1, in ?
      print reduce(lambda x, y: x+y, map(int,fh.xreadlines()))
  TypeError: argument 2 to map() must be a sequence object
  >>> 






More information about the Python-list mailing list