這是我的情況:
我有 x 坐標的變數。.
x_1 = 24
x_2 = 94
x_3 = 120
我有位于這些 x 值之一上的氣象站。
station_1 = 100
station_2 = 80
station_3 = 94
station_4 = 24
station_6 = 120
station_7 = 3
測站 x 坐標保持不變,但 x_1、x_2 和 x_3 坐標不同。它們根據我的腳本的輸入而變化。盡管 x_ 坐標始終與站點坐標之一匹配。
現在我需要找到具有 x_ 坐標的匹配站。我試過這個:
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)
但這行不通。而且它看起來也有點重復。有誰知道如何解決這個問題?也許 for 回圈會有所幫助。
親切的問候,
西蒙
uj5u.com熱心網友回復:
也許我在這里混淆了一些東西,但根據我的理解,你可以在這里使用字典:
dict_st={80:station_4,90:station_3,70:station_2}
x_1_station = dict_st.get(x_1,'no matching stations')
uj5u.com熱心網友回復:
你有沒有考慮在dict這里使用?您可以使用車站坐標作為鍵以確保唯一性,然后將需要從它們存盤的任何資訊作為值。
stations = {
100: station_1_data,
80: station_2_data,
...
}
x1_station = stations.get(x_1)
if x1_station is None:
print("No matching stations")
uj5u.com熱心網友回復:
station_map = {
80: station_1,
100: station_2,
120: station_3,
}
station = mapp.get(x)
這樣寫可能更容易,但我不知道它是否能解決你的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449347.html
