简体   繁体   English

如何使用输入“n”在 python 中定义 n 阶矩阵?

[英]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.

相关问题 如何在python中接受n个列表的输入? - How can take input of n number of lists in python? 如何将一个函数向量化为两个将两个不同长度(m 和 n)序列作为参数并在 Python NumPy 中返回一个矩阵 (mxn)? - How can I vectorize a function to two take as arguments two sequences of different lengths (m and n) and return a matrix (m x n) in Python NumPy? python中的用户输入矩阵(n * n + 1) - User Input matrix(n*n+1) in python 用户在python中输入n*n矩阵 - taking n*n matrix input by user in python Python - 如何使用 python 将 n 乘 n 数据转换为矩阵 - Python - How can I make n by n data to matrix using python Python:如何在具有 N 列的矩阵中转换数组,这些数组是重复 N 次的相同数组? - Python: how can I transform an array in a matrix with N columns, that are the same array repeated N times? 如何使用 python 将用户的 n 值输入到字典中的 n 键? - How Can I input n-values to n-keys in a dictionary from user with python? 如何使用python在n元组矩阵中输入浮点值? - How to input float values in a n-tuple matrix using python? 如何在Python中获得n个数组的nxn协方差矩阵? - How to get n x n covariance matrix for n arrays in Python? 如何用A矩阵返回python中的'A ** n`? - How return `A**n` in python with A an matrix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM