Python 按列顺序打印数字

Python 按列顺序打印数字,python,python-3.x,Python,Python 3.x,我正在Python中的一个模式匹配问题中苦苦挣扎 当input=3时,下面是预期的输出(input value是它应该打印的列数) 预期产出: 1 2 6 3 7 9 4 8 5 我不知何故走错了方向,因此需要一些帮助 这是我迄今为止尝试过的代码: def display(): n = 5 i = 1 # Outer loop for how many lines we want to print while(i<=n)

我正在Python中的一个模式匹配问题中苦苦挣扎

当input=3时,下面是预期的输出(input value是它应该打印的列数)

预期产出:

1
2 6
3 7 9
4 8
5
我不知何故走错了方向,因此需要一些帮助

这是我迄今为止尝试过的代码:

def display(): 
        n = 5
        i = 1
        # Outer loop for how many lines we want to print 
        while(i<=n):  
            k = i 
            j = 1
  
            # Inner loop for printing natural number 
            while(j <= i):  
                print (k,end=" ") 
                  
                # Logic to print natural value column-wise 
                k = k + n - j 
                j = j + 1
                  
            print("\r") 
            i = i + 1
  
#Driver code 
display() 

有人能帮我吗?

这里有一个方法,我从零开始,不是为了代码,对我来说更容易

def构建(注意事项):
值=列表(范围(1,nb_列**2+1))
res=[]
对于范围内的idx(nb_cols):
行值,值=值[-(idx*2+1):],值[:-(idx*2+1)]
res.append(['']*(nb_cols-idx-1)+行值+['']*(nb_cols-idx-1))
对于拉链中的r(*反向(res)):
打印(“.join(map(str,r)))

这里有一个递归解决方案:

def col_counter(start, end):
    yield start
    if start < end:
        yield from col_counter(start+1, end)
        yield start

def row_generator(start, col, N, i=1):
    if i < col:
        start = start + 2*(N - i)     
        yield start
        yield from row_generator(start, col, N, i+1)

def display(N):
    for i, col_num in enumerate(col_counter(1, N), 1):
        print(i, *row_generator(i, col_num, N))
结果

1 
2 20 
3 21 37 
4 22 38 52 
5 23 39 53 65 
6 24 40 54 66 76 
7 25 41 55 67 77 85 
8 26 42 56 68 78 86 92 
9 27 43 57 69 79 87 93 97 
10 28 44 58 70 80 88 94 98 100 
11 29 45 59 71 81 89 95 99 
12 30 46 60 72 82 90 96 
13 31 47 61 73 83 91 
14 32 48 62 74 84 
15 33 49 63 75 
16 34 50 64 
17 35 51 
18 36 
19 
> 

下面是使用简单循环的解决方案

def display(n):
    nrow = 2*n -1  #Number of rows
    i = 1
    noofcols = 1   #Number of columns in each row
    t = 1
    while (i <= nrow):
        print(i,end=' ') 
        
        if i <= n: 
            noofcols = i
        else:
            noofcols = 2*n - i
        m =i
        if t < noofcols:
            for x in range(1,noofcols):
                m = nrow + m -(2*x-1)
                print(m, end=' ')       
            
        i = i+1
        print()
def显示(n):
nrow=2*n-1#行数
i=1
noofcols=1#每行中的列数
t=1
期间(i
n=10
for i in range(1,2*n):
    k=i
    for j in range(2*n-i if i>n else i):
        print(k,end=' ')
        k = k + 2*n - 2*j - 2
    print()
1 
2 20 
3 21 37 
4 22 38 52 
5 23 39 53 65 
6 24 40 54 66 76 
7 25 41 55 67 77 85 
8 26 42 56 68 78 86 92 
9 27 43 57 69 79 87 93 97 
10 28 44 58 70 80 88 94 98 100 
11 29 45 59 71 81 89 95 99 
12 30 46 60 72 82 90 96 
13 31 47 61 73 83 91 
14 32 48 62 74 84 
15 33 49 63 75 
16 34 50 64 
17 35 51 
18 36 
19 
> 
def display(n):
    nrow = 2*n -1  #Number of rows
    i = 1
    noofcols = 1   #Number of columns in each row
    t = 1
    while (i <= nrow):
        print(i,end=' ') 
        
        if i <= n: 
            noofcols = i
        else:
            noofcols = 2*n - i
        m =i
        if t < noofcols:
            for x in range(1,noofcols):
                m = nrow + m -(2*x-1)
                print(m, end=' ')       
            
        i = i+1
        print()