3

I have this list:

list = [[0, 5], [0, 3, 6], [1, 2, 4], [1, 7], [0, 1]]

Each element refers to a point with coordinates listed as two arrays:

Lon = [2.0,3.0,5.0,2.0,6.0,1.0,3.0,4.0]
Lat = [4.0,6.0,5.0,3.0,4.0,2.0,1.0,7.0]

I am trying to create an array that is in the following format:

[[Lon_0,Lat_0],[Lon_5,Lat_5]]
[[Lon_0,Lat_0],[Lon_3,Lat_3],[Lon_6,Lat_6]]
...

I tried the zip function but something is missing and I don't know how to move forward:

for m in list:
    for n in m:
        Coord = zip((Lon[n], Lat[n]))

Any help is appreciated

6
  • 1
    You don't need a for loop, just zip(Lon, Lat) and you're good. Commented Jun 12, 2018 at 14:12
  • 1
    just try zip(Lon, Lat). To turn it into a list use: list(zip(Lon, Lat)). Commented Jun 12, 2018 at 14:12
  • 1
    Oh and something to note is, that zip will turn the "inner" list into a tuple. eg: [(Lon_0, Lat_0), (...), ... ]. Commented Jun 12, 2018 at 14:14
  • Also, consider renaming your "list" from list to something else, say lst. By naming a variable list you override the list constructor. So, for example, @pask's previous suggestion will not work even though it should. Commented Jun 12, 2018 at 14:16
  • But the zip function is not subscriptable, how do I call the zipped coordinate of each point as in the list? Commented Jun 12, 2018 at 14:16

2 Answers 2

1

You can go through each index inside each element of your list and rebuild an entry with the Lon, Lat values.

Here is the code:

# Sample data
list = [[0, 5], [0, 3, 6], [1, 2, 4], [1, 7], [0, 1]]
Lon = [2.0,3.0,5.0,2.0,6.0,1.0,3.0,4.0]
Lat = [4.0,6.0,5.0,3.0,4.0,2.0,1.0,7.0]

# Construct result list
result = []                                                                                                                          
for entry in list:
    elem = []
    for index in entry:
        elem.append([Lon[index], Lat[index]])
    result.append(elem)

# Sample output
print(result)
[[[2.0, 4.0], [1.0, 2.0]], 
 [[2.0, 4.0], [2.0, 3.0], [3.0, 1.0]], 
 [[3.0, 6.0], [5.0, 5.0], [6.0, 4.0]], 
 [[3.0, 6.0], [4.0, 7.0]], 
 [[2.0, 4.0], [3.0, 6.0]]]

In a more "pythonic" way you can use a single-line command (which might be harder to understand but does the exact same thing):

result = [[[Lon[index], Lat[index]] for index in entry] for entry in list]
Sign up to request clarification or add additional context in comments.

1 Comment

@mrcbrbr Glad to hear it. Please consider accepting this answer if it was useful.
1

I'm guessing you need something like this:

[[[Lon[i], Lat[i]] for i in sub] for sub in mylist]
#[[[2.0, 4.0], [1.0, 2.0]],
# [[2.0, 4.0], [2.0, 3.0], [3.0, 1.0]],
# [[3.0, 6.0], [5.0, 5.0], [6.0, 4.0]],
# [[3.0, 6.0], [4.0, 7.0]],
# [[2.0, 4.0], [3.0, 6.0]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.