Andre Garcia
Andre Garcia

Reputation: 914

Create multiple lists inside a list from a list of strings

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

Upvotes: 0

Views: 718

Answers (3)

Mad Physicist
Mad Physicist

Reputation: 114548

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.

Upvotes: 3

I. Milev
I. Milev

Reputation: 1

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

Upvotes: 0

zelenyjan
zelenyjan

Reputation: 703

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

Upvotes: 0

Related Questions