Medicat
Medicat

Reputation: 15

np.subtract() - ValueError subtracting lists with numpy

I gave different groups to loop over. The put_dict and incomes_dict contain a list of Integera for each group. Because the incomes_dict always contain more values, I tried to bring them to the same length. Aim is to subtract the values.

For example:

put_dict[g] = [ 2,3,4]
incomes_dict[g] = [1,2,1]
desired_result[g] = [1,1,3]

The code:

import numpy as np

times = []    
for g in GROUPS:
    laenge = len(put_dict[g])
    times += np.subtract(put_dict[g], incomes_dict[g][:laenge])

The error:

ValueError: operands could not be broadcast together with shapes (0,) (2535,)

Upvotes: 0

Views: 210

Answers (1)

druskacik
druskacik

Reputation: 2497

It's because you are trying to sum a numpy array with a python list, which python doesn't understand.

I guess you want to append all the elements of np.subtract( ... ) to the times array, which you can do by converting the np.subtract( ... ) object to a python list.

import numpy as np

times = []    
for g in GROUPS:
    laenge = len(put_dict[g])
    times += np.subtract(put_dict[g], incomes_dict[g][:laenge]).tolist()

Upvotes: 1

Related Questions