[PYTHON MATRIX-SIG] Array -> String

Konrad Hinsen hinsen@ibs.ibs.fr
Wed, 7 May 1997 17:57:42 +0200


Emmanuel Viennet wrote:

> > Has anyone found an efficient method to convert a character array into
> > a string? The only solution I could come up with is
> > 
> >   string = reduce(operator.add, array)
> > 
> 
>  Can't you use the .tostring() method on array objects ?

I guess I can, meaning it seems to work. But in my mind this is a
low-level function meant for I/O etc.; I am not supposed to make any
assumptions about the internal data organization. Of course in the
case of strings it is hard to see why the internal arrangement should
be anything else than a sequence of characters, but you never know...


Jim Fulton wrote:

> How about 
> 
>   string=string.join(list(array),'')
> 
> Of course, ot would be nicer of string.join accepted any type of
> sequence.

I am not sure this is more efficient than my original solution.
The list must be constructed explicitly first, and string.join
need not be much better than reduce(operator.add,...).


Just in case anyone wonders *why* I am doing this: I need to store
long string in netCDF files, which have no special string type, just
arrays of characters. And those strings can easily reach a length of
500000!

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________