Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

how to make an extend of values in a list without modifying the other values in Python

The problem I have is that in a list that i create with a fusion of two lists with dictionaries, this repeat the values so what I am doing is that I verify which are the ones that are repeated in order to put in the first the missing values in the first record, but when doing this when in the list there are more values with the same values in the second position they are added to all.

import json 

print("Information")
#obtaining the values of the json
obtenerProfesores = json.loads(open("profesores.json").read())
obtenerAsignaturas = json.loads(open("asignaturas.json").read())

def imprimir(listas):
    for x in range(len(listas)):
        print (listas[x])

dataFusion = []
a = 0
# the dictionaries of the jsons are obtained and saved in a fusion list

for dict in obtenerProfesores:
    for dict2 in obtenerAsignaturas:
        nomValue = dict['nombre']
        asigValue = dict['asignaturas']
        dispValue = dict['disponibilidad']
        hrsValue = dict['horas']
        idasigValue = dict2['id']
        nomasigValue =  dict2['nombre']
        hrsemValue = dict2['horas_semana']
        if len(dict['asignaturas']) == 1:
            if dict['asignaturas'][0] == dict2['nombre']:
                dataFusion.append([nomValue, idasigValue])
        if len(asigValue) > 1:       
            for e in dict['asignaturas']:
                if e == dict2['nombre']:
                    dataFusion.append([nomValue, idasigValue])
#here the duplicated data appear but with the part of the different subjects

imprimir(dataFusion)
print("------------------------------------------------------------------")

listaNueva = []
#Attempt to delete repeated data
for i in range(1,len(dataFusion)):
    if( i < len(dataFusion)-1):
        x = dataFusion[i-1]
        x1 = dataFusion[i]
        x2 = dataFusion[i+1]
        if( x[0] == x1[0]):
            x[1].extend(x1[1])
            listaNueva.append(x)
        else:
            if(x[0] != x1[0] and  x2[0] == x1[0]):
                next
            else:
                listaNueva.append(x1)
    else:
        x = dataFusion[i-1]
        x1 = dataFusion[i]
        if( x[0] == x1.keys()[0]):
            x[1].extend(x1[1])
            listaNueva.append(x)
        else:
            listaNueva.append(x1)

        #else:
         #   listaNueva.append(x)

imprimir(listaNueva)

the json are the following asignaturas.json [

{
    "id":["Qu1","Qui2","Qui3"],
    "nombre":"Quimica",
    "grado":1,
    "horas_semana":4,
    "grupo": [1,2,3]
},
{
    "id":["ALi1","ALi2","ALi3"],
    "nombre":"Algebra lineal",
    "grado":1,
    "horas_semana":5,
    "grupo": [1,2,3]
},
{
    "id":["FC1","FC2","FC3"],
    "nombre":"Fundamentos de computo",
    "grado":1,
    "horas_semana":4,
    "grupo": [1,2,3]
},
{
    "id":["AL1","AL2","AL3"],
    "nombre":"Algoritmos",
    "grado":1,
    "horas_semana":7,
    "grupo": [1,2,3]
},
{
    "id":["MD1","MD2","MD3"],
    "nombre":"Matematicas discretas",
    "grado":1,
    "horas_semana":6,
    "grupo": [1,2,3]
},
{
    "id":["EO1","EO2","EO3"],
    "nombre":"Expresion oral y escrita",
    "grado":1,
    "horas_semana":4,
    "grupo": [1,2,3]
},

{
    "id":["CI1","CI2"],
    "nombre":"Calculo integral",
    "grado":4,
    "horas_semana":6,
    "grupo": [4,5]
},
{
    "id":["ISA1","ISA2"],
    "nombre":"Ing Sw Asistido por Computadora",
    "grado":4,
    "horas_semana":6,
    "grupo": [4,5]
}]

Profesores.json

[{  
    "id": "1",
    "profesor":"PA",
    "nombre":"christian baladimir",
    "asignaturas":["Redes", "Algebra lineal"],
    "horas": "15",
    "disponibilidad":
        [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]
},
{
    "id":2,
    "profesor":"PA",
    "nombre":"claudia",
    "asignaturas":["Expresion oral y escrita"],
    "horas":15,
    "disponibilidad":
        [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]
},
{
    "id":3,
    "profesor":"PA",
    "nombre":"Maria Guadalupe",
    "asignaturas":["Quimica", "Calculo integral"],
    "horas":15,
    "disponibilidad":
        [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]
},
{
    "id":4,
    "profesor":"PA",
    "nombre":"Joel",
    "asignaturas":["Algebra lineal", "Fundamentos de computo"],
    "horas":15,
    "disponibilidad":
        [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]
}
]

The result it gives is the following

['christian baladimir', ['ALi1', 'ALi2', 'ALi3']]
['christian baladimir', ['Re1', 'Re2']]
['claudia', ['EO1', 'EO2', 'EO3']]
['Maria Guadalupe', ['Qu1', 'Qui2', 'Qui3']]
['Maria Guadalupe', ['CI1', 'CI2']]
['Joel', ['ALi1', 'ALi2', 'ALi3']]
['Joel', ['FC1', 'FC2', 'FC3']]
['Yolanda', ['ALi1', 'ALi2', 'ALi3']]
['J Bautista', ['MD1', 'MD2', 'MD3']]
['Iracema', ['IE1', 'IE2', 'IE3']]
['Hugo', ['AL1', 'AL2', 'AL3']]
['Hugo', ['ISA1', 'ISA2']]
['Paul', ['ISA1', 'ISA2']]
['Paul', ['CS1', 'CS2', 'CS3']]
['Oscar', ['AL1', 'AL2', 'AL3']]
['Felix', ['RA1', 'RA2', 'RA3']]
['Hector', ['CI1', 'CI2', 'CI3']]
------------------------------------------------------------------
['christian baladimir', ['ALi1', 'ALi2', 'ALi3', 'Re1', 'Re2', 'FC1', 'FC2', 'FC3']]
['claudia', ['EO1', 'EO2', 'EO3']]
['Maria Guadalupe', ['Qu1', 'Qui2', 'Qui3', 'CI1', 'CI2']]
['Joel', ['ALi1', 'ALi2', 'ALi3', 'Re1', 'Re2', 'FC1', 'FC2', 'FC3']]
['Yolanda', ['ALi1', 'ALi2', 'ALi3', 'Re1', 'Re2', 'FC1', 'FC2', 'FC3']]
['J Bautista', ['MD1', 'MD2', 'MD3']]
['Iracema', ['IE1', 'IE2', 'IE3']]
['Hugo', ['AL1', 'AL2', 'AL3', 'ISA1', 'ISA2']]
['Paul', ['ISA1', 'ISA2', 'CS1', 'CS2', 'CS3']]
['Oscar', ['AL1', 'AL2', 'AL3', 'ISA1', 'ISA2']]
['Felix', ['RA1', 'RA2', 'RA3']]
['Hector', ['CI1', 'CI2', 'CI3']]

and what is expected is that it

    ['christian baladimir', ['ALi1', 'ALi2', 'ALi3', 'Re1', 'Re2']]
    ['claudia', ['EO1', 'EO2', 'EO3']]
    ['Maria Guadalupe', ['Qu1', 'Qui2', 'Qui3', 'CI1', 'CI2']]
    ['Joel', ['ALi1', 'ALi2', 'ALi3', 'FC1', 'FC2', 'FC3']]
    ['Yolanda', ['ALi1', 'ALi2', 'ALi3']]
    ['J Bautista', ['MD1', 'MD2', 'MD3']]
    ['Iracema', ['IE1', 'IE2', 'IE3']]
    ['Hugo', ['AL1', 'AL2', 'AL3', 'ISA1', 'ISA2']]
    ['Paul', ['ISA1', 'ISA2', 'CS1', 'CS2', 'CS3']]
    ['Oscar', ['AL1', 'AL2', 'AL3']]
    ['Felix', ['RA1', 'RA2', 'RA3']]
    ['Hector', ['CI1', 'CI2', 'CI3']]

Comments