I want to get the value of pred by putting node1 and node2 value:
THIS MY DATA ARRAY
prediction_data =[
{ "node1": 0, "node2": 1, "pred": 0},
{ "node1": 0, "node2": 476, "pred":0.352956 },
{ "node1": 0, "node2": 494, "pred":0.769988 },
{ "node1": 1, "node2": 505, "pred":0.463901 },
{ "node1": 9, "node2": 68 , "pred":1.238807},
]
THIS WHAT I HAVE TRIED, I'm new in APIs
@app.get("/data/")
async def get_all_predictions(skip: int = 0, limit: int = 10):
return prediction_data[skip : skip + limit]
@app.get("/data/{node1,node2}")
async def getPredOfN1andN2(node1,node2):
if (node1 in prediction_data.node1) and (node2 in prediction_data.node2):
result = prediction_data[pred]
return result
for example, I want to write 0 and 1and get as result pred = 0
You can filter a list by using the built-in filter function:
async def getPredOfN1andN2(node1: int, node2: int):
matching = list(filter(lambda x: x['node1'] == node1 and x['node2'] == node2, prediction_data))
return matching[0]['pred'] if matching else None
If it's possible to have multiple matches, drop the [0] and just return the whole list instead.