等效 C++ python 中的嵌套打印

发布于 2024-10-11 12:02:52 字数 806 浏览 7 评论 0原文

我正在尝试在 python 中运行此 C++ 语句,但在将打印放入循环中时遇到问题。

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        for (int k = 0; k < 3; k++)
        {
          cout<<_3x3x3[i][j][k]<<" ";
        }
    }
     //print here in python
     cout<<endl;
}

在Python中:

_3x3x3 = [[[0 for col in range(3)] for row in range(3)] for depth in range(3)]

for i in range(3):
    for j in range(3):
        for k in range(3):
            _3x3x3[i][j][k] = 1

for col in _3x3x3:
    print col
print
for row in _3x3x3:
    print row
print
for depth in _3x3x3:
    print depth
#I want to print this in the C++ style above
for i in range(3):
    for j in range(3):
        for k in range(3):
            print _3x3x3[i][j][k]

I'm trying to run this C++ statement in python and I'm having trouble placing the print in the loop.

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        for (int k = 0; k < 3; k++)
        {
          cout<<_3x3x3[i][j][k]<<" ";
        }
    }
     //print here in python
     cout<<endl;
}

in python:

_3x3x3 = [[[0 for col in range(3)] for row in range(3)] for depth in range(3)]

for i in range(3):
    for j in range(3):
        for k in range(3):
            _3x3x3[i][j][k] = 1

for col in _3x3x3:
    print col
print
for row in _3x3x3:
    print row
print
for depth in _3x3x3:
    print depth
#I want to print this in the C++ style above
for i in range(3):
    for j in range(3):
        for k in range(3):
            print _3x3x3[i][j][k]

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

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

发布评论

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

评论(2

抚笙 2024-10-18 12:02:52
for i in range(3):
    for j in range(3):
        for k in range(3):
            # something here
            print _3x3x3[i][j][k],
    print
for i in range(3):
    for j in range(3):
        for k in range(3):
            # something here
            print _3x3x3[i][j][k],
    print
滥情空心 2024-10-18 12:02:52

为了多样性和面向未来:

from __future__ import print_function

for i in range(3):
    print(*(_3x3x3[i][j][k] for j in range(3) for k in range(3)))

此代码与 C++ 代码之间的区别在于,它不会在每行末尾、换行符之前输出空格。不过,这可能是一件好事。

For variety, and future-proofing:

from __future__ import print_function

for i in range(3):
    print(*(_3x3x3[i][j][k] for j in range(3) for k in range(3)))

The difference between this and the C++ code is that this doesn't output a space at the end of each line, before the newline. That's probably a good thing, though.

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