Home > database >  python: find value in multiple variables
python: find value in multiple variables

Time:03-18

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

CodePudding user response:

may be i confuse. by my understand you can use dict.

dict_st={80:station_4,90:station_3,70:station_2}
x_1_station = dict_st.get(x_1,'no matching stations')

CodePudding user response:

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

CodePudding user response:

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

  • Related