gpt4 book ai didi

python - 在 python 上的矩阵一侧创建条形

转载 作者:行者123 更新时间:2023-11-30 22:15:54 24 4
gpt4 key购买 nike

我想制作一个矩阵nxn,但在垂直边我想放“|”但我无法使用下面的代码来做到这一点:

def creatematriz(nlines, ncoluns, valor):
M = []
for i in range(nlines):
line = []
for j in range(ncoluns):
line.append(valor)
M.append(line)
return M
def printMatriz(matriz):
for line in matriz:
for position in line:
print(position, end=" ")
print("|")
def main():
m=creatematriz(20,6,'0')
printMatriz(m)
main()

我想要这样的东西:

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

最佳答案

更改您的 printMatriz 函数以在内部循环之前打印 |:

def printMatriz(matriz):
for line in matriz:
print("|", end="")
for position in line:
print(position, end=" ")
print("|")

关于python - 在 python 上的矩阵一侧创建条形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50141807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com