我对Python很陌生,我正在尝试翻译一个Matlab代码。我试图编写一个程序,从用户上传或输入他们的红外训练光谱数据开始,然后程序将其附加到数组或矩阵中。但我不确定我做的是否正确(尤其是因为我总是犯错误!)在
# Requires numpy and math.
# Import necessary modules.
import numpy as np
import math
# Get data for the training spectra as a list.
# Then turn that list into a numpy array given the user's input of how many
# rows and columns there should be.
# (An alternate way to do this would be to have users input it with commas and
# semi-colons.)
# btrain_matrix returns the array.
def btrain_matrix():
btrain = [input("Input btrain as a list of values separated by commas.")]
btrain_row_number = int(input("How many rows should there be in this matrix? \n i.e., how many training samples were there?"))
btrain_column_number = int(input("How many columns should there be in this matrix? \n i.e., how many peaks were trained?"))
btrain_array=np.array(btrain)
btrain_multidimensional_array = btrain_array.reshape(btrain_row_number,btrain_column_number)
print(btrain_multidimensional_array)
return (btrain_multidimensional_array)
btrain_matrix()
btrain_row_number = input("Please re-enter the number of rows in btrain.")
# Insert a sequence to call btrain_matrix here
我得到的错误是:
^{pr2}$如果我输入“1,2,3”和“1”,“1”,程序运行良好。如何让它将这些输入识别为列表中的单独项?在
到目前为止,您的代码还可以,但是如果您使用的是python2.7,
btrain = [input("Input btrain as a list of values separated by commas.")]
将最终成为单个字符串的列表或值的元组列表。正确的方法是split(delimiter)给出在某个分隔符处拆分的所有值的列表(在本例中为“,”
相关问题 更多 >
编程相关推荐