0

I'm trying to built a list of lists in the following format:

coordinates = [[38.768, -9.09], [[41.3092, -6.2762],[42.3092, -6.3762], [41.4092, -6.3762] ...]]

I have 2 lists of strings with the values of latitude and longitude which i want to combine (they are symmetric):

latitude = ['38.768004','41.3092,41.3059,41.3025']     
longitude = ['-9.096851','-6.2762,-6.2285,-6.1809]

I'm using multiple loops trying to build my data and at this point i'm not very confident. I believe there's a better way. How would you do this? Thank you!

The multiple loops i was trying to test are not not valid but here it goes:

    latitude = []
    longitude = []
    processes = dict
    for lat in data['latitude']:
        latitude.append(lat.split(','))
    for lon in data['longitude']:
        longitude.append(lon.split(','))

    print(latitude)
    coordinates = []
    for i in range(len(latitude)):
        # print("Coordinate number: %d" % i)
        for x in range(len(latitude[i])):
            processes[i] = processes[i] + 'teste'
            # years_dict[line[0]].append(line[1])
7
  • Can you show us some of the "multiple loops" you've tried? Commented Feb 13, 2017 at 16:54
  • Can you edit your post to make your list shorter and valid? There are strange quotes. Commented Feb 13, 2017 at 17:05
  • 1
    This is not valid Python code. Please fix. Commented Feb 13, 2017 at 17:07
  • @not_a_robot i appended the code Commented Feb 13, 2017 at 17:12
  • @AndreGarcia. Are you sure your input is formatted correctly? Is it always a single number followed by all the other numbers in a single comma-separated string? Commented Feb 13, 2017 at 17:17

3 Answers 3

3

Assuming that you have two lists of strings, you can do it in a one-liner using zip, float and a list comprehension:

coordinates = [(float(lat), float(lon)) for lat, lon in zip(latitude, longitude)]

If on the other hand your inputs are individual strings with comma-separated values:

coordinates = [(float(lat), float(lon)) for lat, lon in zip(latitude[0].split(','), longitude[0].split(','))]

Finally, if you have a combination of the two, a one-liner is still possible, but will be fairly illegible. A for loop would be easier to read here because you have to unpack the individual substrings:

coordinates = []
for lats, lons in zip(latitude, longitude):
    coordinates.extend((float(lat), float(lon)) for lat, lon in zip(lats.split(','), lons.split(',')))

Here is the one-liner, in case you are interested:

coordinates = [(float(lat), float(lon)) \
         for lat, lon in zip(lats.split(','), lons.split(',')) \
         for lats, lons in zip(latitude, longitude)]

This version uses a generator expression instead of a list comprehension. The syntax is very similar to a list comprehension, but an intermediate list is not created. The items are placed directly into the output using list.extend.

Generally, list comprehensions are much faster than the corresponding for loops because of the way the code works under the hood.

Also, note that I made the coordinate pairs into tuples rather than nested lists. This is just a recommendation on my part.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use something like this. Split strings in lists by comma and then just iterate over lists.

latitude = ['38.768004,41.3092,41.3059,41.3025,41.2991,41.2957,41.2923,41.2888,41.2853,41.2818']
longitude = ['-9.096851,-6.2762,-6.2285,-6.1809,-6.1332,-6.0856,-6.0379,-5.9903,-5.9427,-5.8951']

latitude = latitude[0].split(',')
longitude = longitude[0].split(',')

print [[round(float(latitude[i]), 2), round(float(longitude[i]), 2)] for i, val in enumerate(latitude)]

1 Comment

It's not clear what OP's actual input is, but if your interpretation is correct, nice.
0

There is a built in function called zip which will concatenate the two lists in increasing index value, which you can than iterate and than assign it to list:

coordinates = []
zippedList = zip(map(lambda x: float(x), latitude), map(lambda x: float(x), longitude))
for [a,b] in zippedList:
    coordinates.append([a, b])

2 Comments

This does not convert the strings to numbers
I have fixed that. :)

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.