Skip to main content
 首页 » 编程设计

python之list.append() 的意外输出

2025年05月04日25txw1958

<分区>

我很疑惑:

class lin_reg: 
 
     def __init__(self): 
    ''' Executes the program ''' 
 
    Indep_Array, Dep_Array = self.Prob_Def() 
    Total_Array = Indep_Array.append(Dep_Array) 
    print Indep_Array, Dep_Array, Total_Array 
    NumArray = len(Total_Array) 
 
def Prob_Def(self): 
 
    Analy_Type = raw_input('Type of Regression(linear-n,nonlinear-nl): ') 
    Num_IndepVar = eval(raw_input('Number of Independent Variables: ')) 
    Indep_Array = [] 
    for IndepVar in range(Num_IndepVar): 
        ArrayInput = eval(raw_input('Enter the array: ')) 
        Indep_Array.append(ArrayInput) 
    Dep_Array = eval(raw_input('Enter the dependent array: ')) 
    return Indep_Array, Dep_Array 

当我运行这段代码时,我得到如下输出:

obs=lin_reg.lin_reg() 
Type of Regression(linear-n,nonlinear-nl): nl 
Number of Independent Variables: 3 
Enter the array: [1,2,3] 
Enter the array: [2,3,4] 
Enter the array: [3,4,5] 
Enter the dependent array: [5,6,7] 
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [5, 6, 7]] [5, 6, 7] None 
 
Traceback (most recent call last): 
  File "<pyshell#18>", line 1, in <module> 
    obs=lin_reg.lin_reg() 
  File "C:\Python27\DataAnalysis\lin_reg.py", line 13, in __init__ 
    NumArray=len(Total_Array) 
TypeError: object of type 'NoneType' has no len() 

依赖数组 Dep_Array 如何自动附加到 Indep_Array 以及为什么 Total_Array 返回 None?

对于上述输入,我期待看到这样的输出: [[1,2,3],[2,3,4],[3,4,5]] [5,6,7] [[1,2,3],[2,3,4],[ 3,4,5],[5,6,7]]