0

So I have a 2d array where each element is a dict, like so:

[[{"key":"value"},{"key":"othervalue"}],
[{"key":"notvalue"},{"key":"value"}]]

I'm trying to use that to make a second array, where each element is based on the value of the first array's dictionaries, so it would look something like this:

[[True, True]
[False, True]]

or even this:

[["value","othervalue"]
["notvalue","value"]]

But I cannot for the life of me figure out how to get at the values inside the dictionaries. Right now I'm doing this:

results=numpy.where((old_array=={"key":"value"}) | (old_array=={"key":"othervalue"}))
for i in zip(results[0], results[1]):
    new_array[i[0],i[1]]=True

...but I feel like there's got to be a better way than that. Is there some nice, neat function to use with arrays of dictionaries that I'm completely missing? Or is this just one I'm gonna have to clunk my way through?

Thanks in advance!

3
  • Well, do you know how to get the values of a single dict directly? Do you know how to apply a function to each element of the array (using Numpy rather than explicit looping)? Can you think of a way to put those two things together? Commented Nov 15, 2021 at 2:16
  • @KarlKnechtel If the answer to all of those questions was yes, then I wouldn't be here, would I? I'm still fairly new to python, especially so for NumPy specifically. I've been having a lot of trouble figuring out how to work with entire arrays. arr[i][j]["key"] seems to work sometimes but not when I'm trying to iterate through an array. Commented Nov 15, 2021 at 2:59
  • Well, which question has an answer of no? Did you try to learn how to do the things you can't do? If so, how? For example, did you try putting anything into a search engine? "arr[i][j]["key"] seems to work sometimes but not when I'm trying to iterate through an array" We can only tell you what is wrong with attempts at the code that you show to us. Commented Nov 16, 2021 at 1:03

1 Answer 1

1
In [263]: arr =np.array([[{"key":"value"},{"key":"othervalue"}],
     ...: [{"key":"notvalue"},{"key":"value"}]])
In [264]: arr
Out[264]: 
array([[{'key': 'value'}, {'key': 'othervalue'}],
       [{'key': 'notvalue'}, {'key': 'value'}]], dtype=object)

the straightforward list comprehension approach:

In [266]: [d['key'] for d in arr.ravel().tolist()]
Out[266]: ['value', 'othervalue', 'notvalue', 'value']

a pretty alternative, though not faster:

In [267]: np.frompyfunc(lambda d:d['key'],1,1)(arr)
Out[267]: 
array([['value', 'othervalue'],
       ['notvalue', 'value']], dtype=object)

Object dtype arrays are practically speaking lists, storing references, not special numpy objects. And dict can only be accessed by key indexing (or items/values). numpy does not add any special dict handling code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm still struggling to wrap my head around exactly what ravel() and frompyfunc() actually do but I think I can make it work.

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.