Python 中的字母层

问题描述

如何在不尴尬地重复 elif 行的情况下增加图层?无法让 += 1 工作。或者也许不同的字符串方法?我当然是 Python 新手。

layer = int(input("Give a number between 2 and 26: "))
table_size = layer + layer - 1
ts = table_size
center = (ts // 2)

for row in range(ts):
        for col in range(ts):
                if  row == col == (center):
                        print("A",end="")

                elif  (row > center  or col > center \
                or row < center or col < center) \
                and row < center + 2 and row > center - 2 \
                and col < center + 2 and col > center - 2 :
                        print("B",end="")

                elif  (row > center+1 or col > center+1 \
                or row < center-1 or col < center-1) \
                and row < center+3 and row > center-3 \
                and col < center+3 and col > center-3 :
                        print(chr(67),end="")

                else:
                        print(" ",end="")
        print()

CCCCC
CBBBC
CBABC
CBBBC
CCCCC

解决方法

您可以使用 numpy 来准备字母表的索引,然后使用准备好的索引来获得最终的字符串。方法如下:

# Get your number of layers
N = int(input("Give a number between 2 and 26: "))
assert 2<=N<=26,'Wrong number'

# INDEX PREPARATION WITH NP
import numpy as np
len_vec = np.arange(N)                        
horiz_vec = np.concatenate([np.flip(len_vec[1:]),len_vec]) 
rep_mat = np.tile(horiz_vec,[ 2*N-1,1])
idx_mat = np.maximum(rep_mat,rep_mat.T)

# STRING CREATION: join elements in row with '',and rows with newline '\n'
from string import ascii_uppercase     # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
final_string = '\n'.join(''.join([ascii_uppercase[i] for i in row]) for row in idx_mat)

# PRINTING THE STRING
print(final_string)

N=3 示例:

#> len_vec
array([0,1,2])
#> horiz_vec
array([2,2])
#> rep_mat
array([[2,2],[2,2]])
#> idx_mat
array([[2,2,2]])
#> print(final_string)
CCCCC
CBBBC
CBABC
CBBBC
CCCCC
,

这是一个带有常规 python 列表的示例:

from string import ascii_uppercase

result = []

# Get your number of layers
N = int(input("Give a number between 2 and 26: "))
assert 2<=N<=26,'Wrong number'

for i in range(N):
    # update existing rows
    for j,string in enumerate(result):
        result[j] = ascii_uppercase[i] + string + ascii_uppercase[i]

    # add top and bottom row
    result.append((2*i+1)*ascii_uppercase[i])
    if i != 0:
        result.insert(0,(2*i+1)*ascii_uppercase[i])
        
# print result
for line in result:
    print(line)
,
    layer = int(input("Give a number between 2 and 26: "))
table_size = layer + layer - 1
ts = table_size
center = (ts // 2)
counter=0
print(center)
for row in range(ts):
        for col in range(ts):
                if row<=center and ts-counter>col:
                    outcome=65+center-min(row,col)
                elif row <=center and col>=ts-counter :
                    outcome=65+col-center   
                elif row>center and ts-counter>col:
                    outcome=65+center-min(row,col)  
                elif row >center and col<counter :   
                    outcome=65+row-center
                elif row >center and col>=counter :   
                    outcome=65+row-center+(col-counter)                   
                
                print(chr(outcome),end="")
        counter=counter+1        
               
        print()