简体   繁体   中英

how I can do a request of a symmetrical matrix in python

I'm doing an excel matrix with python by request, but I need to do that the person typing the request put only 1 value and I fill the matrix with the same value in the 2 spots as a symmetrical matrix.

I mean: request only one a and the other one fill it automatically.

在此处输入图像描述

In the code i call nodo to AB C D. I use import openpyxl as opxl to export to excel.

def crear_matriz_adyacente(cant_nodos):
    libro = opxl.Workbook()
    pagina = libro.active
    pagina.title = "matriz_de_adyacencia"
    lista_nodos = []
    cont = 0
 while(cont < cant_nodos):
        contador = str(cont+1)
        nodo = str(input("Ingrese nodo " +contador+ ":"))
        if nodo not in lista_nodos:
            lista_nodos.append(nodo)
            pagina.cell(row = 1, column = cont+2, value = nodo)
            pagina.cell(row = cont+2, column = 1, value = nodo)
            cont = cont+1
        else:
            print("ERROR: Nodo existente, escoja otro: ")
for fila in range(len(lista_nodos)):
        for columna in range(len(lista_nodos)):
       ***     if fila == columna:
                valor = 0  ***
                
            else:
               valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":")) 
            
            while(valor < 0):
                print("ERROR: Valor negativo. Ingrese un valor positivo")
                valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
            
            pagina.cell(row = fila+2, column = columna+2, value = valor)
            
            
    libro.save("matriz_adyacente.xlsx")
    
    return crear_menu()

I think that I need to add some code where i put ***. There I do that only inputs value that aren't in the diagonal, because I fill it with zeros. But I need to only input 1 value of a and the other fill it like I tried to explain before.

I'm sorry about my English and the code. If something isn't clear I can try to explain it better or add more code.

Thank you so much.

For the approach I will be following, the code only the asks the user to input values above the diagonal. One thing to take in mind is that by the symmetry of the problem you're exposing, when user inputs value in entry [i,j] we should add the same value to entry [j,i] . With all this:

for fila in range(len(lista_nodos)):
    for columna in range(len(lista_nodos)):
       if fila == columna:
           valor = 0
       elif columna > fila:
           valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":")) 
           while(valor < 0):
               print("ERROR: Valor negativo. Ingrese un valor positivo")
               valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
           
           pagina.cell(row = fila+2, column = columna+2, value = valor)
           pagina.cell(row = columna+2, column = fila+2, value = valor) 
            
    libro.save("matriz_adyacente.xlsx")
    
    return crear_menu()

Note that this is something pretty similar of what you are doing in

 while(cont < cant_nodos):
        contador = str(cont+1)
        nodo = str(input("Ingrese nodo " +contador+ ":"))
        if nodo not in lista_nodos:
            lista_nodos.append(nodo)
            pagina.cell(row = 1, column = cont+2, value = nodo) # Symmetry is well implemented here!
            pagina.cell(row = cont+2, column = 1, value = nodo)
            cont = cont+1
        else:
            print("ERROR: Nodo existente, escoja otro: ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM