[Python] Modifying/overwriting NumPy array

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    A series of binary NumPy arrays representing cloudy (1's) and clear (0's) sky pixels I added together to find number of observations where cloud is present in each pixel. I want to find percentage of cloud number of cloud (1's)/total observations*100 but cannot get this to work in Python using NumPy arrays as I want to modify original variable:

    import numpy as np

    arr1 = np.array([1,0,1])
    arr2 = np.array([0,0,1])
    total1 = np.add(arr1, arr2)
    >>> [1 0 2]

    total2 = total1 #Purely to make multiple to elaborate my issue
    variables = [total1, total2]

    for x in variables:
    x = x + 100

    total1
    >>> [1 0 2]


    total1 variable has not updated. It works with:

    for x in variables:
    x += 100

    total1
    >>> [201, 200, 202]


    I however do not want to increment by 100. I want to calculate percentage like:

    for x in variables:
    x = x / 1 * 100 #I have simplified the maths for ease of reading

    #This is my desired output - note how total1 is updated, not created
    total1
    >>> [100, 0, 200]


    But this does not work. I am accessing the variables in this way for n variables (variable names go from total1 > total*n*).

    Continue reading...

Compartilhe esta Página