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

这是我得到的txt文件。

......B..
.........
...A.....
.........
.......D.
.........
.C.......
.....E...
.........

从这段文字中,我做了以下的元素,以便从txt文件中的每一个点获取最近的字母表。

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文件中每一项的最近点:我试图做的是首先将列表中每个元素的小写字母打印成行*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'}]

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

我如何才能做到这一点?

解决方案:

如果你只是想打印网格,为什么不直接做这样的事情呢?

N = len(nearest_alphabet_from_point)
W = 9
L = N//W
ind = 0
for i in range(L):
    for j in range(W):
        temp = list(nearest_alphabet_from_point[ind])
        ind += 1
        print(temp[0],end='')
    print('')

輸出

AAAABBBBB
AAAAABBBB
AAAAAABDD
AAAAAADDD
CCAAADDDD
CCCCEEDDD
CCCCEEEDD
CCCEEEEEE
CCCEEEEEE

如果你想构建一个 gridlist 类型,那么你可以做这样的事情。

N = len(nearest_alphabet_from_point)
W = 9
L = N//W
ind = 0
grid = []
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)

本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/209.html

(0)
上一篇 2022年6月29日 下午3:53
下一篇 2022年6月29日 下午3:53

相关推荐

发表评论

登录后才能评论