Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
get value by putting 2 other parameters of the same set from json array in fastAPI

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

  1. this gives me all data
@app.get("/data/")
async def get_all_predictions(skip: int = 0, limit: int = 10):
    return prediction_data[skip : skip + limit]
  1. this is the wanted function but of course the wrong one
@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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda