Home > OS >  creating a dictionary from mapping in python?
creating a dictionary from mapping in python?

Time:12-27

I have the following code that tries to create a dictionary with three keys, and 40 values for each key hopefully. Unfortunately when I try to display the result it only gives the output "<map at 0x7f14048694e0>", and not a dictionary as I intended.

Is there a better way to make a dictionary from the function cat(i) in my code?

data = {}
catchments = 40
randomlist = random.sample(range(2, 2100), catchments)
path = '/uufs/chpc.utah.edu/common/home/u1265332/sliced_catchments/'
def cat(i):
    catch = xr.open_dataset(path   'catch_'   str(i)   '.nc')
    catx = catch.PETNatVeg.mean().values/catch.Prec.mean().values
    caty = catch.TotalET.mean().values/catch.Prec.mean().values
    sf   = catch.snowfall.mean().values/catch.Prec.mean().values
    return {'catx':catx,'caty':caty,'sf':sf}

results = map(cat(i),randomlist)
results

CodePudding user response:

map returns an iterator, meaning that the results are computed when you iterate over it. To get the values, you can convert it to a list:

results = list(map(cat, randomlist))

Note that you just need to pass the function cat to map.

  • Related