Unexpected output from list.append()
Question:
I am puzzled:
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
When I run this code, I get output as follows:
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:Python27DataAnalysislin_reg.py", line 13, in __init__
NumArray=len(Total_Array)
TypeError: object of type 'NoneType' has no len()
How is the dependent array Dep_Array
automatically appended to Indep_Array
and why is Total_Array
returning None
?
I was expecting to see output like this for above input:
[[1,2,3],[2,3,4],[3,4,5]] [5,6,7] [[1,2,3],[2,3,4],[3,4,5],[5,6,7]]
Answers:
.append
modifies the original list and returns None
. You want +
.
I’ve taken the liberty of rewriting your code to how Python should look.
class lin_reg(object):
def __init__(self):
self.indep, self.dep = self.initialise_from_user()
self.total = self.indep + self.dep
print self.indep, self.dep, self.total
self.n = len(self.total)
def initialise_from_user(self):
analysis_type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
n = int(raw_input('Number of Independent Variables: '))
indep = [np.matrix(raw_input('Enter the array: ')) for _ in range(self.n)]
dep = np.matrix(raw_input('Enter the dependent array: '))
return indep, dep
It’s because list.append()
doesn’t return anything, it changes the list in place. Therefore,
Total_Array=Indep_Array.append(Dep_Array)
makes Total_Array
equal None
.
Also, naming conventions are against capitalized variable names.
-
The reason that Dep_Array
is automatically appended to Indep_Array
is because Indep_Array.append(Dep_Array)
modifies Indep_Array
.
-
The function append
returns None
instead of a list. This was done on purpose to prevent people from chaining functions that modify the arguments, such as a.append(b).remove(c).doSomething(d)
. By preventing this, the code remains clear and easier to understand for someone unfamiliar with those methods.
I am puzzled:
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
When I run this code, I get output as follows:
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:Python27DataAnalysislin_reg.py", line 13, in __init__
NumArray=len(Total_Array)
TypeError: object of type 'NoneType' has no len()
How is the dependent array Dep_Array
automatically appended to Indep_Array
and why is Total_Array
returning None
?
I was expecting to see output like this for above input:
[[1,2,3],[2,3,4],[3,4,5]] [5,6,7] [[1,2,3],[2,3,4],[3,4,5],[5,6,7]]
.append
modifies the original list and returns None
. You want +
.
I’ve taken the liberty of rewriting your code to how Python should look.
class lin_reg(object):
def __init__(self):
self.indep, self.dep = self.initialise_from_user()
self.total = self.indep + self.dep
print self.indep, self.dep, self.total
self.n = len(self.total)
def initialise_from_user(self):
analysis_type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
n = int(raw_input('Number of Independent Variables: '))
indep = [np.matrix(raw_input('Enter the array: ')) for _ in range(self.n)]
dep = np.matrix(raw_input('Enter the dependent array: '))
return indep, dep
It’s because list.append()
doesn’t return anything, it changes the list in place. Therefore,
Total_Array=Indep_Array.append(Dep_Array)
makes Total_Array
equal None
.
Also, naming conventions are against capitalized variable names.
-
The reason that
Dep_Array
is automatically appended toIndep_Array
is becauseIndep_Array.append(Dep_Array)
modifiesIndep_Array
. -
The function
append
returnsNone
instead of a list. This was done on purpose to prevent people from chaining functions that modify the arguments, such asa.append(b).remove(c).doSomething(d)
. By preventing this, the code remains clear and easier to understand for someone unfamiliar with those methods.