Rows = int(input("give the number of rows:"))
Columns = int(input("Give the number of columns:"))
matrix = []
for i in range(Rows):
matrix.append(['a', 'b', 'c','d', 'e'])
for vector in matrix:
print(matrix)
输出如下:
give the number of rows:3
Give the number of columns:3
[['a', 'b', 'c', 'd', 'e']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
[当用户输入行和列3x3时需要这样]
a b c
d e f
g h i
有很多方法可以初始化一个特定大小的数组。下面是一个更简洁的方法。
Rows = int(input("Give the number of rows:"))
Columns = int(input("Give the number of columns:"))
matrix = [["a"]*Rows]*Columns
print(matrix)
这将输出
Give the number of rows:3
Give the number of columns:3
[['a', 'a', 'a'], ['a', 'a', 'a'], ['a', 'a', 'a']]
给出你想要的数组大小。