Angel
Angel

Reputation: 2865

How to iterate over a list of list of lists of lists of lists... of list of pair of coordinates in python?

I am trying to iterate over a list of lists of lists... of list of pair of coordinates in python and I want to sum a value to each of these pair of coordinates but I also want to keep the structure.

I think an example is worth more than a thousand words so:

coordinates = [[[-15.418887, 28.180395], [-15.418887, 28.180395]],
 [[-15.794088, 28.018681], [-15.794088, 28.018681]]]

This is a very basic example but is not the real case. In the real each of the list has variables lengths except for the pair of coordinates. So maybe there are 4 lists until the list of list of pair coordinates or it could be 3, it is variable

I want to add 3 (for example) to each of these coordinates but keeping the original structure ( I don't want to flat the list of lists of list ....)

Maybe this is useful for trying to answer the question:

Any help is appreciated.

Upvotes: 3

Views: 302

Answers (4)

user2390182
user2390182

Reputation: 73450

General map approach to apply a function only to the innermost elements of a randomly deeply nested list:

def map_nested(fnc, lst):
    if not isinstance(lst, list):
        return fnc(lst)
    return [map_nested(fnc, x) for x in lst]
    # or for in-place mutation:
    # lst[:] = (map_nested(fnc, x) for x in lst)
    # return lst

add3 = lambda x: x+3

map_nested(add3, coordinates)
# [[[-12.418887, 31.180395], [-12.418887, 31.180395]], 
#  [[-12.794088, 31.018681], [-12.794088, 31.018681]]]

Upvotes: 1

Mark
Mark

Reputation: 92440

You can use a recursive function to handle the general case. Just check if you have a list and recurse, otherwise return the result of the addition:

def add_to_list(l, n):
    if not isinstance(l, list):
        return l + n
    return [add_to_list(sub, n) for sub in l]
   
coordinates = [[[-15.418887, 28.180395], [-15.418887, 28.180395]],[[-15.794088, 28.018681], [-15.794088, 28.018681]]]

add_to_list(coordinates, 3)    
#[[[-12.418887, 31.180395], [-12.418887, 31.180395]], [[-12.794088, 31.018681], [[-12.794088, 31.018681]]]]

# A more general case:
add_to_list([1, [2, [5, [6]]], 3], 2)
# [3, [4, [7, [8]]], 5]

# Degenerate cases
add_to_list([], 2)
# []
add_to_list(5, 2) 
# 7

This assumes that your nested data will either be numbers or lists.

Upvotes: 1

SiAce
SiAce

Reputation: 391

coordinates = [[[-15.418887, 28.180395], [-15.418887, 28.180395]],
               [[-15.794088, 28.018681], [-15.794088, 28.018681]]]

def recursively_add3(l):
    if isinstance(l, float):
        return l
    
    for index, inner_list in enumerate(l):
        inner_result = recursively_add3(inner_list)
        if inner_result is not None:
            l[index] = inner_result + 3
    
    return None

recursively_add3(coordinates)

which gives the result

[[[-12.418887, 31.180395], [-12.418887, 31.180395]]
, [[-12.794088, 31.018681], [-12.794088, 31.018681]]]

Upvotes: 1

juan villarejo
juan villarejo

Reputation: 47

You are using a 3D vector (lists of lists of lists) of coordinates right? Then your loop should be triple to access to all the variables on that 3D vector.

for x in list: #x is a list of lists
  for y in x: #y is a list
    for z in y: #z is a pair of coordinates
      #Your code here

Upvotes: 0

Related Questions