Python "table" 或 "matrices" 项目
Python "table" or "matrices" project
我的代码需要一个建议。在学校,我们正在学习如何使用 python 制作表格。
我正在尝试用“1”完成反对角线,但我不知道该怎么做!
nbr_co = int(input("number of columns : "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range():
grid[i][] = 1
grid[][i] = 1
affiche(grid)
这是一种输出:
[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
你可以这样做:
nbr_co = int(input("number of columns : "))
nbr_li = int(input("number of lines: "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range(nbr_co):
grid[i][i] = 1
affiche(grid)
如果您想要另一条对角线,请这样做:
nbr_co = int(input("number of columns : "))
nbr_li = int(input("number of lines: "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range(nbr_co):
grid[i][(nbr_co - 1) - i] = 1
affiche(grid)
我明白你想做什么。
首先,如果要操作对角矩阵的逆矩阵,行数和列数必须相同。
在下面的示例中,您将找到您正在尝试执行的操作的示例。
nbr_column = 4 #you can chose any number you want.
grid = [] # it's your table.
for i in range(nbr_column):
row = [] # this variable will present a row of your table.
for j in range(nbr_column):
row.append(0)
grid.append(row)
""" to display your table"""
for i in grid:
print(i)
""" to change diagonal with 1 """
for i in range(len(grid)):
grid[i][i] = 1
""" to display your new table"""
for i in grid:
print(i)
我的代码需要一个建议。在学校,我们正在学习如何使用 python 制作表格。 我正在尝试用“1”完成反对角线,但我不知道该怎么做!
nbr_co = int(input("number of columns : "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range():
grid[i][] = 1
grid[][i] = 1
affiche(grid)
这是一种输出:
[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
你可以这样做:
nbr_co = int(input("number of columns : "))
nbr_li = int(input("number of lines: "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range(nbr_co):
grid[i][i] = 1
affiche(grid)
如果您想要另一条对角线,请这样做:
nbr_co = int(input("number of columns : "))
nbr_li = int(input("number of lines: "))
grid = [[0]*nbr_co for i in range(nbr_li)]
def affiche(grid):
for i in range(nbr_li):
print(grid[i])
for i in range(nbr_co):
grid[i][(nbr_co - 1) - i] = 1
affiche(grid)
我明白你想做什么。 首先,如果要操作对角矩阵的逆矩阵,行数和列数必须相同。
在下面的示例中,您将找到您正在尝试执行的操作的示例。
nbr_column = 4 #you can chose any number you want.
grid = [] # it's your table.
for i in range(nbr_column):
row = [] # this variable will present a row of your table.
for j in range(nbr_column):
row.append(0)
grid.append(row)
""" to display your table"""
for i in grid:
print(i)
""" to change diagonal with 1 """
for i in range(len(grid)):
grid[i][i] = 1
""" to display your new table"""
for i in grid:
print(i)