Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

creating a dictionary from mapping in python?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading