1

I have initialized a 2D array (235, 451) using

grid = np.zeros((235,451)) 

A snippet of the array is:

a = array(Counter({(87, 177): 347, (72, 191): 335, (88, 178): 318, (68, 188): 318, (67, 188): 318, (67, 187): 314}),dtype=object)

I want to populate the following indices with the counter values and leave all the other values without a value as zero.

e.g.

>>> grid[0,0] = 0
>>> grid[87,177]=347
>>> grid[72,191]=335 

My overall goal will be to then contour this over a map. Thank you.

3
  • Why is your Counter instance in an np.array in the first place? And you probably didn't initialize your grid like that... Commented Jul 13, 2017 at 17:55
  • Only because I'm more familiar with using numpy, so I changed the counter to a numpy array Commented Jul 13, 2017 at 17:58
  • You didn't change it to a numpy array, you just put it inside one (the dtype is object) Commented Jul 13, 2017 at 17:59

1 Answer 1

4

Don't put your counter in an array and do

from collections import Counter
import numpy as np

a = Counter({(87, 177): 347, (72, 191): 335, (88, 178): 318, (68, 188): 318, (67, 188): 318, (67, 187): 314})
keys = np.array(list(a.keys()))
values = np.array(list(a.values()))

grid = np.zeros((235,451))
grid[keys[:, 0], keys[:, 1]] = values
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.