-1

Okay so here I have a list of weather stations, their coordinates in lat and lon all in different lists.

# Station names
stations = ['Hanko Russarö', 'Heinola Asemantaus', 'Helsinki Kaisaniemi', 
            'Helsinki Malmi airfield', 'Hyvinkää Hyvinkäänkylä', 'Joutsa Savenaho', 
            'Juuka Niemelä', 'Jyväskylä airport', 'Kaarina Yltöinen', 'Kauhava airfield', 
            'Kemi Kemi-Tornio airport', 'Kotka Rankki', 'Kouvola Anjala', 
            'Kouvola Utti airport', 'Kuopio Maaninka', 'Kuusamo airport', 
            'Lieksa Lampela', 'Mustasaari Valassaaret', 'Parainen Utö', 'Pori airport', 
            'Rovaniemi Apukka', 'Salo Kärkkä', 'Savonlinna Punkaharju Laukansaari', 
            'Seinäjoki Pelmaa', 'Siikajoki Ruukki', 'Siilinjärvi Kuopio airport', 
            'Tohmajärvi Kemie', 'Utsjoki Nuorgam', 'Vaala Pelso', 'Vaasa airport', 
            'Vesanto Sonkari', 'Vieremä Kaarakkala', 'Vihti Maasoja', 'Ylitornio Meltosjärvi']

# Latitude coordinates of Weather stations  
lats = [59.77, 61.2, 60.18, 60.25, 60.6, 61.88, 63.23, 62.4,
       60.39, 63.12, 65.78, 60.38, 60.7, 60.9, 63.14, 65.99,
       63.32, 63.44, 59.78, 61.47, 66.58, 60.37, 61.8, 62.94,
       64.68, 63.01, 62.24, 70.08, 64.5, 63.06, 62.92, 63.84,
       60.42, 66.53]

# Longitude coordinates of Weather stations 
lons = [22.95, 26.05, 24.94, 25.05, 24.8, 26.09, 29.23, 25.67, 
       22.55, 23.04, 24.58, 26.96, 26.81, 26.95, 27.31, 29.23, 
       30.05, 21.07, 21.37, 21.79, 26.01, 23.11, 29.32, 22.49, 
       25.09, 27.8, 30.35, 27.9, 26.42, 21.75, 26.42, 27.22, 
       24.4, 24.65]

How can I combine these so that I can figure out which stations of the list belong to a certain lat, lon area? I'll do at loop of

N = len(stations)
print(N)
34

but how do I then use that loop to find out the station, lat and lon pairs? Do I use some kind of index method?

Alexandra
  • 93
  • 2
  • 6
  • Okay the header got a bit confusing, since I know the conditional statement, the problem is the pairing of the lists. – Alexandra Sep 25 '18 at 16:04
  • It would be nice if you showed us what you have tried so far. Even simple brute force iteration over the length of the `stations` list should allow you to index against the other lists. There are other more 'pythonic' methods of course. But it looks like you have not even tried anything. – David Zemens Sep 25 '18 at 16:12

5 Answers5

3

You can do something like this

for combo in zip(stations,lats,lons):
    print(combo)
SuperStew
  • 2,857
  • 2
  • 15
  • 27
3

Create a dictionary with the stations and their coordinates

You can use the built-in function zip() to combine these lists and then iterate over them:

locations = {}

for station, latitude, longitude in zip(stations, lats, lons):
   locations[station] = latitude, longitude

This will result in a dictionary with the following format:

locations = {
  "Hanko Russarö" : (59.77, 22.95),
  "Heinola Asemantaus" : (61.2, 26.05),
  ...
}

Of course this can also be written on a single line

locations = { station : (lat, lon) for station, lat, lon in zip(statios, lats, lons)}

Creating the dictionary with additional conditions

This can be combined with some conditions, for example:

locations = { station : (lat, lon) for station, lat, lon in zip(statios, lats, lons) if lat > 50}

or

locations = {}

for station, latitude, longitude in zip(stations, lats, lons):
    if latitude > 50:
        locations[station] = latitude, longitude
kalehmann
  • 4,821
  • 6
  • 26
  • 36
1

Some good solutions here, but you may also be interested in using pandas DataFrames to manage your data:

import pandas as pd
df = pd.DataFrame({
    'station': stations,
    'lat': lats,
    'lon': lons
})
print(df)
Ben DeVries
  • 506
  • 3
  • 6
0

If you know you have lists of equal lengths:

for i in range(len(stations)):
    print(stations[i], lats[i], lons[i])

To answer your second question

Could the conditional statement be integrated into this? for example: i for i in lats if i < 64.5?

Yes, just add a condition before printing.

for i in range(len(stations)):
    if lats[i] > 64.5:
        print(stations[i], lats[i], lons[i])
Michael Cox
  • 1,116
  • 2
  • 11
  • 22
0

You can use the zip() function to iterate over multiple iterables:

for station, lat, lon in zip(stations, lats, lons):
    print(f'Station {station} with latitude {lat} and lon {lon}')
Franndy Abreu
  • 186
  • 2
  • 12