xxx
xxx
xxx
试图找到制作这个2D列表的方法。
答案 0 :(得分:2)
这在我创建 2048 游戏板时对我有用
# creating a board of 4 x 4 size
# initializes a list of lists with 4 rows and 4 columns
board = [ [0] * 4 ] * 4
# for printing the board
for row in board:
print(row)
# the result it produces
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
答案 1 :(得分:1)
试试这个:
def main():
print("Num of rows:")
row = int(input())
print("Num of Cols:")
columns = int(input())
print("Out:\n")
for i in range(row): # iterate in rows
print('x'*columns) # print 'x' columns times
if __name__ == '__main__':
main()
答案 2 :(得分:0)
执行此操作的众多方法之一:
parser.h
例如,for x in range(row):
print('x '*col)
,row =3
然后输出:
col =3
<强>替代地强>,
创建一个列表 -
x x x
x x x
x x x
然后打印 -
a=[]
for x in range(0, row):
a.append(["x"] * col)