2

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?

1
  • You can use, but you are using it wrong. np.concatenate. Change the line to: np.concatenate(rate, r, axis = 0) change the axis according to your need. Commented Oct 21, 2019 at 11:18

2 Answers 2

1
Docstring:
concatenate((a1, a2, ...), axis=0, out=None)
a1, a2, ... : sequence of array_like
    The arrays must have the same shape, except in the dimension
    corresponding to `axis` (the first, by default).

This is a function, not a method. It is called with np.concatenate.

The first argument is a tuple (or more generally sequence) of arrays (or array like). If called with np.concatenate(a1, a2), the a2 will be interpreted as the axis parameter, which must be a simple number!

Don't use np.concatenate (or np.append) as though it were a clone of list append. alist.append(r) is a method call, and acts in-place. The numpy functions are functions and don't act in-place. They return a new array. When used repeatedly in a loop they are much less efficient.

From your description, this sounds like a simple list comprehension problem:

In [14]: alist = [[1,2],[],[2,3],[],[],[4]]                                     
In [15]: newlist = [i if len(i) else [0] for i in alist]                        
In [16]: newlist                                                                
Out[16]: [[1, 2], [0], [2, 3], [0], [0], [4]]

Or written as a for loop:

In [20]: newlist = [] 
    ...: for i in alist: 
    ...:     if len(i)==0: 
    ...:         i = [0] 
    ...:     newlist.append(i) 

This list could be turned into an array with one (correct) np.concatenate call after:

In [22]: np.concatenate(newlist)                                                
Out[22]: array([1, 2, 0, 2, 3, 0, 0, 4])
Sign up to request clarification or add additional context in comments.

Comments

1

To concatenate two numpy arrays, you have to write rate = np.concatenate((rate,r),axis=0/1), depending upon how you want to concatenate the two arrays.

3 Comments

I changed the code according to what you had suggested and I still have the following error: TypeError: only integer scalar arrays can be converted to a scalar index
Can't anyone read the np.concatenate documentation? What are its required arguments?
you are probably missing the double brackets and may be giving np.concatenate (a,b) and the interpreter is assuming the second vector to be an axis(a scalar). Please see Kapil's solution carefully or read the documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.