JoVe
JoVe

Reputation: 495

Convert nested lists to arrays in python

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] ] ]

Upvotes: 1

Views: 838

Answers (4)

questionto42
questionto42

Reputation: 9532

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])]]

Upvotes: 0

R3n3
R3n3

Reputation: 31

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]

Upvotes: 1

Quinn
Quinn

Reputation: 4504

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]]]

Upvotes: 1

tobspr
tobspr

Reputation: 8376

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

Upvotes: 1

Related Questions