[英]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;我通过键盘输入请求的数字不是 5、9、12、14、15; I put: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15我输入: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. cant_nodos = 6 在这种情况下。 (is an input) (是输入)
Python request me all the cells of the area of upper diagonal and fill the downside automatically. Python 请求我上对角线区域的所有单元格并自动填充下边。
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.我正在为 excel 使用 openpyxl。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.