Pass the array into the function and traverse, why the element used to traverse becomes the entire array

pass the array into the function and traverse, arr is the np.array array, t is the number

related codes

def timereduce(arr,t):
    print(arr)
    for q in arr:
        q-=t
        if q<0:
            q=0
    print(q)
    return arr

error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any () or a.all ()
I output Q and find that Q becomes the entire array, which is why

Jul.28,2021

Use a.any () or a.all ()

has given you a hint, use any or all to manipulate arr
do not change the value of the arr element during traversal, it is too inefficient.


doesn't understand your problem.
but from the code alone.
the temporary variable Q in your code will only print the result of the last loop. It should be the difference between the last element in the arr array and the number t (if less than 0, then 0).
finally, return the original array arr, note that it is the original array, because there are no changes to the original array in the code.
as far as this function is concerned, There is no error.
are there any conflicts or other problems with other variables? Please check more


def timereduce(arr, t):
    return [max(q-t, 0) for q in arr]
Menu