Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在电路板中插入阵列_Python - Fatal编程技术网

Python 在电路板中插入阵列

Python 在电路板中插入阵列,python,Python,我试图用这段代码将数组“a”放入这个板中,但我只得到一个只有数组第一个数字的板,似乎变量“I”上缺少和递增,但我不能在join附近放置没有语法错误的递增,有人能帮忙吗?提前谢谢。代码如下: `a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] i = 0 def inc_i(i): i += 1 return i def board_

我试图用这段代码将数组“a”放入这个板中,但我只得到一个只有数组第一个数字的板,似乎变量“I”上缺少和递增,但我不能在join附近放置没有语法错误的递增,有人能帮忙吗?提前谢谢。代码如下:

`a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

i = 0


def inc_i(i):
    i += 1
    return i

def board_draw(height, width, i):
    top = "┌" + "┬".join(["─"*6]*width) + "┐\n"
    bottom = "└" + "┴".join(["─"*6]*width) + "┘"
    middle = "├" + "┼".join(["─"*6]*width) + "┤\n"
    print(top +
          middle.join(
              "│" +
              "│".join('  {:02d}  '.format(a[i])
                       for y in range(width)) +
              "│\n"

              for x in range(height)) +

          bottom)

board_draw(5, 5, 0)`

我希望数组中的每个单元格都有一个不同的数字


提前感谢

这是您想要的退货吗

a = [[25, 24, 23, 22, 21], [20, 19, 18, 17, 16], [15, 14, 13, 12, 11], [10, 9, 8, 7, 6], [5, 4, 3, 2, 1]]

i = 0

def inc_i(i):
    i += 1
    return i


def board_draw(height, width, i):
    top = "┌" + "┬".join(["─"*6]*width) + "┐\n"
    bottom = "└" + "┴".join(["─"*6]*width) + "┘"
    middle = "├" + "┼".join(["─"*6]*width) + "┤\n"
    print(top +
          middle.join(
              "│" +
              "│".join('  {:02d}  '.format(a[x][y])
                       for y in range(width)) +
              "│\n"

              for x in range(height)) +

          bottom)


board_draw(5, 5, 0)

您可能希望将列表
a
转换为:

a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

a_bis = [[n for n in a[i:i+4]] for i in range(5)]
a_bis

我在任何时候都不会被增加。要保持程序的“逻辑”,请尝试以下操作:

a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

def board_draw(height, width):
    top = "┌" + "┬".join(["─"*6]*width) + "┐\n"
    bottom = "└" + "┴".join(["─"*6]*width) + "┘"
    middle = "├" + "┼".join(["─"*6]*width) + "┤\n"
    print(top +
          middle.join(
              "│" +
              "│".join('  {:02d}  '.format(a[(x*width) + y]) for y in range(width)) +
              "│\n" for x in range(height)) +
              bottom
          )

board_draw(5, 5)

我修改并使用了您已经定义的函数
inc_I()
来迭代列表并打印正确的结果:

a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

i = -1
def inc_i():
    global i
    i += 1
    return i

def board_draw(height, width, i):
    top = "┌" + "┬".join(["─"*6]*width) + "┐\n"
    bottom = "└" + "┴".join(["─"*6]*width) + "┘"
    middle = "├" + "┼".join(["─"*6]*width) + "┤\n"
    print(top +
          middle.join(
              "│" +
              "│".join('  {:02d}  '.format(a[inc_i()]) for y in range(width)) +
              "│\n" for x in range(height)) +
          bottom)

board_draw(5, 5, 0)

这是因为
i
永远不会递增。这正是我想要的!谢谢
a = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

i = -1
def inc_i():
    global i
    i += 1
    return i

def board_draw(height, width, i):
    top = "┌" + "┬".join(["─"*6]*width) + "┐\n"
    bottom = "└" + "┴".join(["─"*6]*width) + "┘"
    middle = "├" + "┼".join(["─"*6]*width) + "┤\n"
    print(top +
          middle.join(
              "│" +
              "│".join('  {:02d}  '.format(a[inc_i()]) for y in range(width)) +
              "│\n" for x in range(height)) +
          bottom)

board_draw(5, 5, 0)