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 >>>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4