I have written some simple code to iterate through a group of lists I am analyzing (from b1 to b20). To these lists, I want to check first which of them are empty. To those that are empty, I want to add the value 0. I want to add 0 to the empty lists, because I will later sum the values from different lists altogether and, as far as I understand, I cannot add together lists that are empty.
At the moment, I have the following code:
for z in np.arange(1,21):
r=np.array([0])
rate = eval('b' + str(z))
print (z)
if len(rate)==0:
rate.concatenate(r)
print (rate)
else:
print (rate)
order_x20=b16+c16+d16+h16+i16
order_x2020=b17+c17+d17+h17+i17
order_x2050=b15+c15+d15+h15+i15
order_x20100=b2+c2+d2+h2+i2
order_x20300=b20+c20+d20+h20+i20
Every time I run the code, I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-37-876cc7bddcdf> in <module>
2200 print (z)
2201 if len(rate)==0:
-> 2202 rate.concatenate(r)
2203 print (rate)
2204 else:
AttributeError: 'numpy.ndarray' object has no attribute 'concatenate'
Could someone please help me solve the issue? I don't really understand why I am getting this error, but I assume is due to the fact that I cannot use np.append() or np.concatenate() with the eval() function?
np.concatenate. Change the line to:np.concatenate(rate, r, axis = 0)change the axis according to your need.