1

I would like to know if it is possible to have "nested arrays", that is to say an array that contains arrays that have different shapes.

I have a list of lists of coordinates, so something like:

coord = [ [ [x1,y1],[x2,y2] ], [ [x3,y3],[x4,y4],[x5,y5] ], [ [x6,y6] ] ]

I would like to convert all these lists into arrays, so I can do mathematical operations with it. The result would be a (3,)-array containing 3 arrays (one at each position) of respective shapes (2,2) (corresponding to the nested list [ [x1,y1],[x2,y2] ]), (3,2) and (1,2).

The final goal is to be able do to something like result = coord + [x7,y7], to beneficiate from the properties of matricial operations in Python (I was told that it was much more efficient than doing loops, and I have a lot of coordinates).

The result would be:

result = [ [ [x1+x7,y1+y7],[x2+x7,y2+y7] ], [ [x3+x7,y3+y7],[x4+x7,y4+y7],[x5+x7,y5+y7] ] ]

JoVe
  • 435
  • 2
  • 9
  • 17
  • 1
    If you would like to convert all these lists into arrays, what does the result look like? – Quinn Apr 29 '16 at 14:25
  • Suggest you read [_How do I ask a good question?_](http://stackoverflow.com/help/how-to-ask) – martineau Apr 29 '16 at 15:28
  • Can you explain why ma question is bad ? I'm a beginner in Python, so in my mind it seems clear but maybe it doesn't make sense. @ccf the result would be a (3,)-array containing 3 arrays of respective shapes (2,2), (3,2) and (1,2) – JoVe Apr 29 '16 at 15:48
  • If you need MATRIXES in python, don't ask a question about array. That will mislead everyone. – mootmoot Apr 29 '16 at 15:59
  • Ok, I thought matrixes were arrays in python. Besides, I don't need "matrixes" in the mathematical sense. I want to use the python array properties, like adding a (3,) vector to a (3,3)-array (will add the vector to the rows of the array, term by term). – JoVe May 02 '16 at 08:39

4 Answers4

1

If you have coordinates, then you probably want to use your custom class for storing them. The following won't work as intended, assuming coord is [x1, x2] then

 result = coord + [x7,y7]

will yield:

 result = [x1, x2, x7, y7]

What you should consider doing is to write your own Coordinate class for example, and override the operators (i.e. __add__), for example:

class Coordinate(object):
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __add__(self, other):
        return Coordinate(self.x + other.x, self.y + other.y)

    # ...

Also see A guide to pythons magic methods

tobspr
  • 8,200
  • 5
  • 33
  • 46
  • The problem is I have to store the coordinates in nested lists (the overlying problem is more complicated but it's useless to explain). I edited the question, I hope it is clearer – JoVe Apr 29 '16 at 15:50
1

You could use map to do the conversion:

coord = map (lambda c: [ [xy[0] + x7, xy[1] + y7] for xy in c], coord )

Code sample:

# some example coordinates
x1,y1 = 1,1
x2,y2 = 2,2
x3,y3 = 3,3
x4,y4 = 4,4
x5,y5 = 5,5
x6,y6 = 6,6
x7,y7 = 7,7
coord = [  [ [x1,y1],[x2,y2] ], [ [x3,y3],[x4,y4],[x5,y5] ], [ [x6,y6] ]  ]
# the result is:
coord = map (lambda c: [ [xy[0] + x7, xy[1] + y7] for xy in c], coord )
print (coord)

[Output]

[[[8, 8], [9, 9]], [[10, 10], [11, 11], [12, 12]], [[13, 13]]]
Quinn
  • 4,394
  • 2
  • 21
  • 19
1

First convert the list of lists into a list of numpy matrices (matrix_ls):

coord = [  [ [ 1, 1],[ 2, 2] ], [ [ 3, 3],[ 4, 4],[ 5, 5] ], [ [ 6, 6] ]  ]

import numpy as np
matrix_ls = list(map(lambda m_ls: np.matrix(m_ls), coord))

Then you can apply all kinds matrix operations from NumPy Manual Here is an example with summation:

sum_matrix = np.matrix([10,10]) # [x7,y7]
result = [matrix + sum_matrix for matrix in matrix_ls]
R3n3
  • 31
  • 6
0

You try to

beneficiate from the properties of matricial operations,

but your main aim is to

convert all these lists into arrays, so I can do mathematical operations with it.

List comprehension is much faster than a coded loop, though it is basically a "for" loop as well, see Why is a list comprehension so much faster than appending to a list?. You can combine list comprehension with list conversion into numpy arrays (matrices are just multi-dimensional arrays, while we only use one-dimensional arrays for the calculations), and it might even do well on a bigger dataset.

It is probably slower than a pure matrix solution that avoids any loop, that is why I might miss the point of the question here.

coord = [  [ [ 1, 1],[ 2, 2] ], [ [ 3, 3],[ 4, 4],[ 5, 5] ], [ [ 6, 6] ]  ]
x7 = 1
x8 = 1
[[np.array(np.array(a) + np.array([x7,x8])) for a in x] for x in coord]

Output:

[[array([2, 2]), array([3, 3])],
 [array([4, 4]), array([5, 5]), array([6, 6])],
 [array([7, 7])]]
questionto42
  • 7,175
  • 4
  • 57
  • 90