Movatterモバイル変換
[0]ホーム
Congruence of 'map' and 'for' broken in 2.1??
nanotech at europa.comnanotech at europa.com
Fri Apr 27 16:19:44 EDT 2001
All:OK, now that my subject line hooked you :) I am trying to determinewhy I can not use fh.xreadlines() in map:Say I want to sum the integers in a file where there exists one intper 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 fudginggoing on here ....BUT....Say I want to do the same thing in the nice simple example given inthe 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 objectAck!! Why will map not fudge? What am I missing?? Is it pilot error??Thanks!! QuentinDETAIL: >>> 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-listmailing list
[8]ページ先頭