Hello I have two objects in python, the first one:
completos = [
{
"id": 83,
"bloque": "Proyectos",
"atributo": "fecha_cierre",
"orden": 1,
"visible_cv_resumido": true,
"visible_cv_completo": true,
"mapeo": "fecha_cierre",
"administrador": 1
},
{
"id": 84,
"bloque": "Proyectos",
"atributo": "id",
"orden": 1,
"visible_cv_resumido": true,
"visible_cv_completo": false,
"mapeo": "id",
"administrador": 1
},
{
"id": 85,
"bloque": "Proyectos",
"atributo": "fecha_inicio",
"orden": 1,
"visible_cv_resumido": true,
"visible_cv_completo": true,
"mapeo": "fecha_inicio",
"administrador": 1
},
]
And another fix like this:
data = [
{
"id": 1,
"fecha_inicio": "2005-02-01",
"fecha_cierre": "2012-01-01",
"codigo_proyecto": "",
"nombre_proyecto": "Diferentes dentro del del Conocimiento Tradicional del Pueblo Saraguro",
"descripcion": "",
"tipo_proyecto": 65,
},
{
"id": 3,
"fecha_inicio": "2008-01-01",
"fecha_cierre": "2012-06-30",
"codigo_proyecto": "PROY_CBCM_0011",
"nombre_proyecto": "PROSPECCION DE PRINCIPIOS ACTIVOS ANTICANCEROSOS DE LA FLORA DEL SUR DEL ECUADOR.",
"descripcion": "El cancer constituye una de las principales causas de muerte, se estima que para el 2020 cerca de 13 millones de personas falleceran por esta enfermedad. Por tanto, el cancer constituye un problema de salud mundial y ocupa un lugar de importancia en lo",
"tipo_proyecto": 65,
}
]
How can I do that based on the field visible_cv_completo
of the complete array, if that field is true I can obtain the information for example "atributo": "fecha_cierre"
from the data array and obtain "fecha_cierre": "2012-01-01"
or "atributo": "fecha_inicio"
and from the data array obtain "fecha_inicio": "2005-02-01"
In the first array called complete I have in the field atributo
what I want to obtain from the array data. I do not know how to do it. I hope you can help me. Thanks in advance.
I understand that what you are looking for is the following:
completos
get the list of attributes that should be visible. According to your example, that list would be['fecha_cierre', 'fecha_inicio']
because they are the ones withTrue
the key"visible_cv_completo"
data
, copy all the dictionaries it contains, but leaving in each one of these dictionaries only the keys of the previously obtained list (that is, only the keys"fecha_cierre"
and"fecha_inicio"
).This can be accomplished with a couple of list comprehensions.
Extract the list of visible attributes
This one is pretty straightforward: we iterate through the list
completos
getting one dictionary at ad
time, and if in that dictionary the key"visible_cv_completo"
is truthy , we add to the resultd["atributo"]
Filter dictionaries from the second list
This one is a bit more complicated:
Here again we iterate, this time over list
data
, getting a dictionaryd
on each iteration. Based on that dictionary wed
build another, using the expression{atributo: d.get(atributo) for atributo in visibles}
. This other expression is a dictionary comprehension , where I iterate through the attributes in the listvisibles
and for each attribute I get its value from the dictionaryd
currently being processed in the "main" loop.Applying this code to your example (after editing your example so that it is correct Python, which it is not, since you have to capitalize the values
True
andFalse
), the following result appears in the listfiltrados
:Which I hope is what you were looking for.