[英]Trying to print the below Grid in Python
I am trying to print a string as a Grid as per below image.我正在尝试根据下图将字符串打印为网格。
I understand there will be a for loop and I have got the Grid display elements as:
我知道会有一个 for 循环,并且我将 Grid 显示元素设置为:
HORIZONTAL_WALL = "-"
VERTICAL_WALL = "|"
CORNER = "+"
EMPTY = " "
for i in range(len(puzzle)):
Would anybody please help me by showing a way to get this done.有人可以通过展示完成这项工作的方法来帮助我吗?
Thanks谢谢
If you are ok with the python external packages, here is a way of doing that如果您对 python 外部包没问题,这是一种方法
termtables: https://github.com/nschloe/termtables术语表: https://github.com/nschloe/termtables
!pip install termtables
import termtables as tt
string = "nevagonagiveu up"
# Python3 program to Split string into characters
def split(word):
return [char for char in word]
puzzle = split(string)
Convert into a 2d array using numpy使用 numpy 转换为二维数组
import numpy as np
puzzle_arr = np.array(puzzle).reshape(-1, 4)
puzzle_arr
grid = tt.to_string(
puzzle_arr,
style=tt.styles.ascii_thin,
# alignment="ll",
# padding=(0, 1),
)
print(grid)
Output: Output:
+---+---+---+---+
| n | e | v | a |
+---+---+---+---+
| g | o | n | a |
+---+---+---+---+
| g | i | v | e |
+---+---+---+---+
| u | | u | p |
+---+---+---+---+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.