[英]How can I take an input 'n' to define a matrix of order n in python?
num_array = list()
num = input("Enter how many elements you want:")
print('Enter numbers in array: ')
for i in range(int(num)):
n=input("num :")
num_array.append(int(n))
print('ARRAY: ',num_array)
this one was there but it's not gonna give me matrix of order n这个在那里,但它不会给我 n 阶矩阵
I think if you want a matrix representation, you should go with a list of lists.我想如果你想要一个矩阵表示,你应该 go 和一个列表列表。 You only input n numbers, but for a matrix you need n*n numbers.
您只输入 n 个数字,但对于矩阵,您需要 n*n 个数字。 Do that with a second for loop like so:
像这样使用第二个 for 循环来做到这一点:
# matrix is gonna be a list of lists
num_array = list()
num = input("Enter how many elements you want:")
print('Enter numbers in array: ')
# first for iterates for rows
for i in range(int(num)):
row = list()
# second for iterates numbers in every row
for j in range(int(num)):
n = input("num :")
row.append(int(n))
num_array.append(row)
# output as matrix
for row in num_array:
for number in row:
print(number, end=" ")
print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.