0

this is my situation:

I have variables of x coordinates. .

x_1 = 24
x_2 = 94
x_3 = 120

And I have weather stations that are located on one of these x values.

station_1 = 100
station_2 = 80
station_3 = 94
station_4 = 24
station_6 = 120
station_7 = 3

The station x coordinates stay the same but not the x_1, x_2 and x_3 coordinates. They change depending on the input of my script. Although the x_ coordinates always matches one of the station coordinates.

Now I need to find the matching station with the x_ coordinate. I tried this:

if x_1 == 100:
    x1_station = station_1
elif x_1 == 80:
    x1_station = station_1
elif x_1 == 94:
    x1_station = station_1    
elif x_1 == 24:
    x1_station = station_1
elif x_1 == 120:
    x1_station = station_1
elif x_1 == 3:
    x1_station = station_1
else:
    print("no matching stations")
    
print(x1_station)

But this will not work. And it also looks a bit of to much repetition. Does anyone know how to solve this? Maybe a for loop would help.

Kind regards,

Simon

4
  • 1
    What does "this will not work" mean? Commented Mar 18, 2022 at 8:31
  • @simonDL can you verify if your provided code is correct? Looks like those section_1 variables need to be changed in if conditions Commented Mar 18, 2022 at 8:35
  • @Hammad, yes you are right. I made this as an example code and got a bit confused, sorry. But now I edited it right. But if I have to use this code on like 10 x coordinates the code gets very long. I think there should be a better way to do this. Commented Mar 18, 2022 at 8:44
  • looks like you should really use a container, having numbered variables should be replaced by a list or dictionary Commented Mar 18, 2022 at 8:50

3 Answers 3

2

Maybe I confuse something here, but from my understanding you could use a dictionary here:

dict_st={80:station_4,90:station_3,70:station_2}
x_1_station = dict_st.get(x_1,'no matching stations')
Sign up to request clarification or add additional context in comments.

Comments

2

Have you considered using a dict here? You could use the station coordinates for the keys to ensure uniqueness and then have whatever information you need to store from them as the values.

stations = {
    100: station_1_data,
    80: station_2_data,
    ...
}

x1_station = stations.get(x_1)
if x1_station is None:
    print("No matching stations")

Comments

0
station_map = {
    80: station_1,
    100: station_2,
    120: station_3,

}
station = mapp.get(x)

It might be easier to write it this way, but I don't konw if it will solve your problem

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.