[Tutor] Question concerning splitting of arrays
Kent Johnson
kent_johnson at skillsoft.com
Wed Sep 29 14:38:52 CEST 2004
Derek,
You should look into the numarray package, it can do what you want. See
http://stsdas.stsci.edu/numarray/numarray-1.1.html/node26.html for some
examples.
In standard Python, lists of lists are used to represent multi-dimensional
arrays. Operations on multidimensional arrays are applied sequentially to
the lists that represent the arrays. As you found out, this is not really a
good model for full-featured matrix operations. Here is what is happening:
>>> array = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
array is a list of three elements. Each element is also a list of three
elements.
What does this mean?
>>> array[1][2]
23
array[1][2] is the same as (array[1])[2]. It takes element 1 of array,
which is the list [21, 22, 23], then takes element 2 of that list, which is
23. This is what you expect.
What about this?
>>> array[:][0]
[11, 12, 13]
Again, array[:][0] is the same as (array[:])[0]. array[:] is a copy of all
of array! So you are copying array, then taking the first element of the
copy. Not what you wanted at all.
Finally, how about this?
>>> array[1:][1:]
[[31, 32, 33]]
As before, array[1:][1:] is (array[1:])[1:]. array[1:] is everything but
the first element of array; this is a list containing two lists:
>>> array[1:]
[[21, 22, 23], [31, 32, 33]]
Now you take the slice [1:] of that list, and you are dropping the first
element again. array[1:][1:] is the same as array[2:]!
Kent
At 05:18 PM 9/29/2004 +0930, Weber, Derek wrote:
>Hi all,
>
>I'm actually doing something like translating some matlab code to Python
>and am wondering whether it's possible to split multi-dimensional arrays
>as such:
>
> >>> array = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
> >>> firstColumn = array[:][0]
> >>> lowerRightCorner = array[1:][1:]
> >>> print firstColumn
>[[11], [21], [31]]
> >>> print lowerRightCorner
>[[22, 23], [32, 33]]
>
>I seem to get errors when I do this, so I thought I'd ask people who knew
>a great deal more about the language than I did to see if there's an easy
>way to do this, or do I have to write some functions to do it for me?
>
>I think the issue is making the first array reference a range.
>
>Any suggestions would be gratefully accepted. Also, if there's a way to
>search the archive of the list, I'd do that, but I haven't had any luck
>with the one referred to by python.org.
>
>Thanks very much.
>
>D.
>
>============================================================
>Derek Weber derek.weber at dsto.defence.gov.au
>Information Exploitation Group Rm: 2.H.06 205Labs
>Defence Science & Technology Organisation Ph: 61 8 8259 7699
>PO Box 1500, Edinburgh SA 5111 Fx: 61 8 8259 5619
>============================================================
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list