1

I have an empty 'numpy.ndarray'

import numpy as np
my_grid =  np.zeros((5, 5))

parse = "max","min","avg"

I wish to create a dictionary where each element of parse is the "Key"

from collections import defaultdict

GridMetric = dict()
for arg in parse:
    GridMetric[arg].append(my_grid)

but i get this error

   Traceback (most recent call last):
  File "<editor selection>", line 3, in <module>
KeyError: 'max'
11
  • You can't use a numpy array as the key of a dictionary (just like you can't use a list or other mutable object). Commented Mar 3, 2013 at 17:21
  • Would you give an exact code which produces this error, so that we see exactly what is parse here. Commented Mar 3, 2013 at 17:26
  • Do you want to create a separate dictionary "for each element of parse" or do you want to create dictionary in which items in parse are keys, and the values are ... what? Commented Mar 3, 2013 at 17:30
  • parse is the key. Sorry if i explain bad the question Commented Mar 3, 2013 at 17:33
  • 3
    It looks like you want GridMetric = defaultdict(list). Commented Mar 3, 2013 at 19:12

2 Answers 2

3

If what you want is a dictionary whose keys are the different elements of the list called parse and whose values are all the same array, then the following changes to your code should work:

import numpy as np
my_grid =  np.zeros((5, 5))

parse = ["max","min","avg"]

d = {}
for arg in parse:
    d[arg] = my_grid
Sign up to request clarification or add additional context in comments.

5 Comments

What is the problem with this code? You said "you get it" when I wrote "every key (different elements of the list, parse) will have the same value (the same array, my_grid)?" In what way does the code provided not do that for you?
i got always Traceback (most recent call last): File "<editor selection>", line 3, in <module> KeyError: 'max' becose i need parse as "max", "min", "avg"
@Gianni: that's because you imported defaultdict from collections, but aren't using it -- your object is a dict, although it looks like you want defaultdict(list), and so there's none of the default-creation behaviour. You say you want the grid to be the value, but instead you're going to have values which are lists, the first element of which is my_grid.
@user2074981: I suspect the OP really wants my_grid.copy(), even though I agree that you asked exactly this question -- whether he wanted each value to be the same array -- and he said yes.
@DSM It's true. I forgot GridMetric =defaultdict(list) and I made this mistake. Sorry and thanks for your time and help.
1

Try this:

import numpy as np

my_grid =  np.zeros((5, 5))

parse = ["max","min","avg"]


for arg in parse:
    dict(parse=my_grid)

print(d)

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.