简体   繁体   中英

request symmetrical matrix in python

I'm doing some code to create a symmetrical matrix by request but I have an issue.

This is the code that I have. *** Is the code that maybe I have wrong and don't know how to fix it. I had try many variations. And the numbers that I put in the request by keyboard aren't 5,9,12,14,15; I put: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

But the matrix created is this:

cant_nodos = 6 in this case. (is an input)

在此处输入图像描述

Python request me all the cells of the area of upper diagonal and fill the downside automatically.

    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
            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) ***

I need to create a symmetrical matrix. I'm using openpyxl for excel.

Thank you for your help..

I fixed. If someone have the same problem, this was the solution:

 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
       ***     
            pagina.cell(row = fila+2, column = columna +2, value = valor) ***
            
        elif columna > fila:
            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) ***
                  
        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]+ ":"))
        
        
        
libro.save("matriz_adyacente.xlsx")

return crear_menu()

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