get-a-cup-of-coffee slow (fwd)

Alex Martelli aleaxit at yahoo.com
Fri Aug 10 04:18:52 EDT 2001


"Mark Rowe" <mark21rowe at yahoo.com> wrote in message
news:3b737128 at news.actrix.gen.nz...
> Hi,
>
> The first example should be written:
>
> >>> l=[0,1,2,3,4]
> >>> f=open('e', 'w')
> >>> f.write(''.join(l))
>
> and the second should  be something like:
>
> >>> l=[0,1,2,3,4]
> >>> f=open('e', 'w')
> >>> l2 = map(str, l)
> >>> f.writelines(l2)
>
> This is because f.write() takes a expects a string argument, not a list,
and
> f.writelines() expects a list of strings, not a list of integers.

Yes, but remember the original question, which you quote below:

> presumably more
> > > >|suited to being appended), but then I realized that I didn't know
how
> to write
> > > >|the contents of a list to a file as a stream of bytes.  There's

"a stream of bytes" suggests that the posted wanted to use chr, not
str, as the function to be mapped.  Anyway, array.array has far better
performance for this task.


Alex






More information about the Python-list mailing list