[Numpy-discussion] two dimensional array of sets
Benjamin Root
ben.root@ou....
Thu Aug 12 18:05:19 CDT 2010
On Thu, Aug 12, 2010 at 2:59 PM, gerardob <gberbeglia@gmail.com> wrote:
>
> Hello, this is a very basic question but i don't know the answer.
>
> I would like to construct a two dimensional array A, such that A[i][j]
> contains a set of numbers associated to the pair (i,j). For example,
> A[i][j]
> can be all the numbers that are written as i^n*j*5-n for some all n=1,..,5
>
> Is numpy what i need? if not which is the way to define such arrays. I
> mean,
> how do i declare an 2-dimensional array of a given size (for after start
> filling it with specific sets)?
>
> Thanks.
>
>
> Because you want to hold a set, it might possibly be better to use python
lists and list comprehensions. However, you can use numpy to help you
out...
import numpy as np
rows = 5
cols = 5
n = np.arange(1, 6)
a = [[set((i**n) * (j*(5 - n))) for j in range(1, cols+1)] for i in
range(1,rows+1)]
The first row is a[0] (which is i == 1) :
[set([0, 1, 2, 3, 4]), set([8, 0, 2, 4, 6]), set([0, 9, 3, 12, 6]), set([16,
8, 12, 4, 0]), set([0, 10, 20, 5, 15])]
I hope this helps,
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100812/baf42cdd/attachment.html
More information about the NumPy-Discussion
mailing list