DimKoim
DimKoim

Reputation: 1044

IndexError: invalid index to scalar variable

I have created the following function:

def c_min(a,b):
    result= [x - y for x in a for y in b]
    min=np.min(result)
    return min

I have created a test file with two lists:

a=[1,2,3] and b=[4,5,6]

When I running the function I get the correct result.

However when I run the function in my initial code I have the aforementioned in the title error. Have to be noted that the function in the initial code is applied in two arrays. One has size 1 and it is numpy.float64,the other 3 and it is numpy.ndarray. Why it can be applied to list and not in the arrays?

Upvotes: 0

Views: 12009

Answers (2)

DimKoim
DimKoim

Reputation: 1044

As mentioned in the post using the numpy.arrayor numpy.ndarraythe problem can be solved.

Upvotes: 0

damienfrancois
damienfrancois

Reputation: 59130

Both a and b need to be numpy.ndarray. If one of them is a scalar, the for ... in ... construct will fail.

Upvotes: 1

Related Questions