Q: How do I make a for loop to each individual array? I have a function that I created and I want the function to be applied to these different values using a for loop or something.
How do I create a for loop that takes each value but stores them in different arrays?
I have this so far:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.patches as patches 
import xarray as xr 
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import netCDF4 as s
import numpy.ma as ma

fwf_tot = fwf_ice      + ds.runoff_tundra*ds.LSMGr #data input i am using 


# function i want to apply to the data

def ob_annual(ob_monthly, id_number):
    ann_sum =  ob_monthly.where(ds.ocean_basins == id_number).resample(TIME='1AS').sum().sum(dim=('X','Y'))
    return ann_sum 

This is where my problem is to create the for loop to save for these different values. I think this for loop is just saving the function applied to the last value (87) and not the others. How might I fix this? I expected there to be an output of 7 arrays with each a size of 59.
obs = np.array([26,28,29,30,76,84,87])

total_obs = []

for i in obs:
    total_obs = ob_annual(fwf_tot_grnl, i)
    
print(total_obs.shape) 

(59)


A: You replace your list total_obs at each iteration. You must append each value into it:
for i in obs:
    total_obs.append(ob_annual(fwf_tot_grnl, i))

or use a comprehension list
total_obs = [ob_annual(fwf_tot_grnl, i) for i in obs]

