[PYTHON MATRIX-SIG] array()

Konrad Hinsen hinsen@ibs.ibs.fr
Tue, 13 May 1997 17:43:12 +0200


>     I have two ways to do what I want:
> 
> 	a = array(['a', 'bcd', 'fg', 'h'], 'O')[:,0]
>     
>     or
> 
> 	a = zeros(4, typecode='O')
> 	a[:] = ['a', 'bcd', 'fg', 'h']
> 
>     Both work, but seem perverse to me.

I'd use the following:

   a = array(4*[None])
   a[:] = ['a', 'bcd', 'fg', 'h']

Which of course is rather close to your second solution, but a bit
clearer in my opinion.

> (2) I have two one-dimensional arrays of python objects a and b (same length)
>     and I wish to create a one-dimensional array where each element is
>     the list (or tuple) containing the corresponding pair of objects
>     from a and b.  Note that I want the one-dimensional array of lists
>     and not the two-dimensional array of python objects. I can't seem
>     to make this happen without a loop which is slow and lacking
>     elegance.  In case you wonder why, I wish to sort the one-d array,
>     in other words to sort on more than one key.

You'd be better off using standard lists for that purpose:

      l = map(None, a, b)
      l.sort()

If you need to you can always convert back to an array at the end.
-- 
-------------------------------------------------------------------------------
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
_______________