因此,我正在尝试创建一个网格,可以将其单独的“网格方块”替换为任何给定的符号。网格工作正常,但它由列表中的列表组成。
这是代码
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
feild[i].append("#")
feild[4][4] = "@" #This is one of the methods of replacing that I have tried
for i in range(size):
p_feild = str(feild)
p_feild2 = p_feild.replace("[", "")
p_feild3 = p_feild2.replace("]", "")
p_feild4 = p_feild3.replace(",", "")
p_feild5 = p_feild4.replace("'", "")
print(p_feild5)
正如您所看到的,这是我尝试替换元素的一种方式,我也尝试过:
feild[4[4]] = "@"
和
feild[4] = "@"
第一个用“@”替换左边的所有“#”4个元素 第二个给出以下错误
TypeError: 'int' object is not subscriptable
答案 0 :(得分:1)
制作#
网格,第3行第3列替换为@
:
>>> size = 5
>>> c = '#'
>>> g = [size*[c] for i in range(size)]
>>> g[3][3] = '@'
>>> print('\n'.join(' '.join(row) for row in g))
# # # # #
# # # # #
# # # # #
# # # @ #
# # # # #
答案 1 :(得分:0)
可能你正在寻找: -
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
map(feild[i].append, ["#" for _ in xrange(size)])
i = 4
feild[i][0] = "@"