与矩阵及其位置斗争

发布于 2025-02-03 19:37:00 字数 265 浏览 2 评论 0原文

我正在尝试解决这个程序(python),其中有一个矩阵,我应该显示每个行,每个位置和他的价值

我正在尝试这句话,但它不起作用:

m = [
    [3, 8, 2],
    [2, 3, 1],
    [4, 2, 9]
]



for i in range(len(m)):
    print('the row number',i+1,'and the position',m[0][i],'has a value of',m[i][0])

I am trying to solve this program (python) where having a matrix I should show every row, every position and his value

I am trying this sentence but it is not working:

m = [
    [3, 8, 2],
    [2, 3, 1],
    [4, 2, 9]
]



for i in range(len(m)):
    print('the row number',i+1,'and the position',m[0][i],'has a value of',m[i][0])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2025-02-10 19:37:00

您应该有一个嵌套循环以在每个原始和每个col上打印

m = [
    [3, 8, 2],
    [2, 3, 1],
    [4, 2, 9]
]

for i in range(len(m)):
    for j in range(len(m[i])):
        print('the row number', i + 1, 'and the position', j + 1, 'has a value of', m[i][j])

the row number 1 and the position 1 has a value of 3
the row number 1 and the position 2 has a value of 8
the row number 1 and the position 3 has a value of 2
the row number 2 and the position 1 has a value of 2
the row number 2 and the position 2 has a value of 3
the row number 2 and the position 3 has a value of 1
the row number 3 and the position 1 has a value of 4
the row number 3 and the position 2 has a value of 2
the row number 3 and the position 3 has a value of 9

You should have a nested loop to print on each raw and each col:

m = [
    [3, 8, 2],
    [2, 3, 1],
    [4, 2, 9]
]

for i in range(len(m)):
    for j in range(len(m[i])):
        print('the row number', i + 1, 'and the position', j + 1, 'has a value of', m[i][j])

which gives

the row number 1 and the position 1 has a value of 3
the row number 1 and the position 2 has a value of 8
the row number 1 and the position 3 has a value of 2
the row number 2 and the position 1 has a value of 2
the row number 2 and the position 2 has a value of 3
the row number 2 and the position 3 has a value of 1
the row number 3 and the position 1 has a value of 4
the row number 3 and the position 2 has a value of 2
the row number 3 and the position 3 has a value of 9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文