Reputation: 29
When I compile it this error appears. ValueError: max() arg is an empty sequence.
ag_averaged = [max(a_g[i_averaged[i]:i_averaged[i+1]]) for i in range(len(i_averaged)-1)]
Honestly I have no idea why this ocurrs
Upvotes: 0
Views: 2055
Reputation: 3194
As the error message says, it's because the sequence that you passed to max
has zero length. In your case, i_averaged[i]
might be equal to i_averaged[i + 1]
on some occasion, which gives a slice of length zero. To prevent it from raising an exception, you can provide a default value for the max
function:
max(a_g[i_averaged[i]:i_averaged[i+1]], default=None)
Now it returns None
when the list is empty. Of course, it might not benefit you as the maximum value of a list of zero length might not be meaningful in your application. It is now your responsibility to ensure that the value makes sense.
Upvotes: 2