adding matrices of strings
Robert Kern
kern at caltech.edu
Thu Oct 5 23:43:50 EDT 2000
In article <39DBA479.1179C63E at northwestern.edu>, Louis Luangkesorn
<lluang at northwestern.edu> wrote:
[snip]
> It does not seem like the NumPy array accepts strings, and I was
> thinking about adding lists of lists together, but all I get is the set
> of lists in A with the lists in B.
You can use NumPy. Just use the "O" typecode when creating the matrix
with the "array" function. The caveat is that when you pull an element
out of the array by indexing it, the value returned is not a Python
string, but another NumPy array. Use str(A[i,j]) to convert.
>>> from Numeric import array
>>> a = array([['a', 'b'],['c','d']], 'O')
>>> a
array([[a , b ],
[c , d ]],'O')
>>> a + a
array([[aa , bb ],
[cc , dd ]],'O')
>>> type(a[0, 0])
<type 'array'>
>>> str(a[0, 0])
'a'
> the other thing I need to do is to take that matrix A, and pull a
> particular element from it (without removing that element like a
> a.pop(x) would do)
A[i,j] with NumPy arrays. With matrices built from lists, A[i][j]
should work.
--
Robert Kern
kern at caltech.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
More information about the Python-list
mailing list