以字符串网格格式打印集合列表

这是我得到的txt文件:


......B..

.........

...A.....

.........

.......D.

.........

.C.......

.....E...

.........

从这个文本中,我制作了以下元素,用于从 txtfile 中的每个点获取壁橱字母表。


location_alpha ={(2, 3): 'A', (0, 6): 'B', (6, 1): 'C', (4, 7): 'D', (7, 5): 'E'}


location_allpoint = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]

我尝试做的是将txt文件中的所有点更改为最接近的字母并以字符串格式打印:


   aaaabbBbb

   aaaaabbbb

   aaaAaabdd

   aaaaaaddd

   ccaaaddDd

   cccceeddd

   cCcceeedd

   ccceeEeee

   ccceeeeee

为此,我得到的是 txt.file 中每个项目的最近点:我试图做的是首先将列表中每个元素的小写打印为 row*col 字符串网格格式,例如上面的示例,然后更改原始字母表的索引为大写。


 for i in nearest_alphabet_from_point: 

      if len(i) ==2: # one that comes first lexicographically must be used in case ex){'A', 'C'} ==> 'A'


nearest_alphabet_from_point =[{'A'}, {'A'}, {'A'}, {'A'}, {'B'}, {'B'}, {'B'}, {'B'}, {'B'}, {'A'}, {'A'}, {'A'}, {'A'}, {'A'}, {'B'}, {'B'}, {'B'}, {'B'}, {'A'}, {'A'}, {'A'}, {'A'}, {'A'}, {'A'}, {'B'}, {'D'}, {'D'}, {'A', 'C'}, {'A'}, {'A'}, {'A'}, {'A'}, {'D', 'A'}, {'D'}, {'D'}, {'D'}, {'C'}, {'C'}, {'A', 'C'}, {'A'}, {'A'}, {'D'}, {'D'}, {'D'}, {'D'}, {'C'}, {'C'}, {'C'}, {'C'}, {'E'}, {'E'}, {'D'}, {'D'}, {'D'}, {'C'}, {'C'}, {'C'}, {'C'}, {'E'}, {'E'}, {'E'}, {'D'}, {'D'}, {'C'}, {'C'}, {'C'}, {'E'}, {'E'}, {'E'}, {'E'}, {'E'}, {'E'}, {'C'}, {'C'}, {'C'}, {'E'}, {'E'}, {'E'}, {'E'}, {'E'}, {'E'}]

但我从第一步开始就无法以字符串网格格式打印它。


我怎样才能做到这一点?


慕妹3242003
浏览 90回答 1
1回答

富国沪深

如果您只想打印网格,为什么不做这样的事情呢?N = len(nearest_alphabet_from_point)W = 9L = N//Wind = 0for i in range(L):    for j in range(W):        temp = list(nearest_alphabet_from_point[ind])        ind += 1        print(temp[0],end='')    print('')输出AAAABBBBBAAAAABBBBAAAAAABDDAAAAAADDDCCAAADDDDCCCCEEDDDCCCCEEEDDCCCEEEEEECCCEEEEEE如果要构造 agrid的list类型,则可以执行以下操作:N = len(nearest_alphabet_from_point)W = 9L = N//Wind = 0grid = []for i in range(L):    row = []    for j in range(W):        ch = list(nearest_alphabet_from_point[ind])[0]        if (i,j) not in location_alpha:            ch = ch.lower()        row.append(ch)        ind += 1    grid.append(row)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python