如何在 Python 3 中生成行和列

新手上路,请多包涵

我需要知道如何生成一个数字序列,这些数字 在行(6 行)列(7 行) 中以设置的字段宽度(2)和空间(1) _右对齐_。

First I have to ask the user for an input number "n" (with requirements -6 < n < 2 ) and then print "n to n+6" in the first column, "n+7 to n+13" 在下一栏中等等。

我从以下代码中获得了输入/输出,但不知道如何生成行和列(或指定字段宽度):

 n = int(input("Enter the start number: "))

if n>-6 and n<2:
    for x in range(n, n+41):
        print(n, end=" ")
        n = n+1

我一直在尝试在这个 for 循环中使用 for 循环来完成它,但无法弄清楚。任何帮助将不胜感激 - 在此先感谢您!

原文由 Josh Alexandre 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 477
2 个回答

最高数只能有两位所以你只需要右对齐2,使用第一个循环的步长6并从第一个开始从每个x开始内循环,我们还需要在n等于-5时捕捉, 38 步长为 6 总是给我们 7 列和六行,除非 n 是 -5 那么我们需要使用 37 所以减去 (n < -4) 当 n 为 38 -> 37 -5 或者什么都不带走:

 n = int(input("Enter the start number: "))

if -6 < n < 2:
    for x in range(n, 38 - (n < -4), 6):
        for j in range(x,  x + 6):
            print("{:>2}".format(j), end=" ")
        print()

将它放在函数 pr_right 中并从 -5 运行到 1:

 -5 -4 -3 -2 -1  0
 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

-4 -3 -2 -1  0  1
 2  3  4  5  6  7
 8  9 10 11 12 13
14 15 16 17 18 19
20 21 22 23 24 25
26 27 28 29 30 31
32 33 34 35 36 37

-3 -2 -1  0  1  2
 3  4  5  6  7  8
 9 10 11 12 13 14
15 16 17 18 19 20
21 22 23 24 25 26
27 28 29 30 31 32
33 34 35 36 37 38

-2 -1  0  1  2  3
 4  5  6  7  8  9
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27
28 29 30 31 32 33
34 35 36 37 38 39

-1  0  1  2  3  4
 5  6  7  8  9 10
11 12 13 14 15 16
17 18 19 20 21 22
23 24 25 26 27 28
29 30 31 32 33 34
35 36 37 38 39 40

 0  1  2  3  4  5
 6  7  8  9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
36 37 38 39 40 41

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
37 38 39 40 41 42

还有其他更简单的方法,但我想这是某种学习练习。

如果实际上是六行七列就更简单了:

 for x in range(n, 37, 7):
    for j in range(x,  x + 7):
        print("{:>2}".format(j), end=" ")
    print()

如果我们通过另一个 pr_right 函数输出运行它:

 In [10]: for n in range(-5, 2):
             pr_right(n)
             print()
   ....:
-5 -4 -3 -2 -1  0  1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 32 33 34 35 36

-4 -3 -2 -1  0  1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 32 33 34 35 36 37

-3 -2 -1  0  1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
32 33 34 35 36 37 38

-2 -1  0  1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 32
33 34 35 36 37 38 39

-1  0  1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 32 33
34 35 36 37 38 39 40

 0  1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34
35 36 37 38 39 40 41

 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42

原文由 Padraic Cunningham 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以将 numpy 用于 reshape 。这给出了一个简洁且更具可读性的代码:

 import numpy as np

n = int(input("Enter the start number: "))

if n>-6 and n<2:
    print (n + np.arange(42)).reshape(6,7)

原文由 Colonel Beauvel 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题