2

This is surely a silly mistake, as I was not aware of the dict.copy functionality. The problem is that I had a dictionary full of data, say x, that I copied to another variable, say y, by doing y=numpy.copy(x). The thing is that later I saved the variable y and wrote it to a pickle (and I repeated this several times, writing several files; its part of a very long simulation!). Now when I open the pickle, I get an ndarray object which contains my dictionary but inside a ndarray object, and I have no idea on how to retrieve the original dictionary with the original data. A working example:

import numpy
x = {'a':[1,2,3], 'b':['foo','bar']}
y = numpy.copy(x)

I have tried lots of things with no success; is there actually a way to retrieve the original dictionary, x, from the new variable y?

Thanks in advance for the help!

Néstor
  • 585
  • 8
  • 20

2 Answers2

3

In my testing somehow y.tolist() gives the original dict back. Maybe try that :)

Some more sensible methods perhaps I got from this question:

y[()]

And:

y.item()
Community
  • 1
  • 1
  • I can't believe this didn't occured to me, thank you so much! – Néstor Dec 03 '14 at 20:01
  • @Néstor, Well a method with a name `tolist` that returns a dict strikes me as somewhat odd –  Dec 03 '14 at 20:08
  • 1
    But is somewhat intuitive, as working with lists is always by far less complicated than with numpy arrays. – Néstor Dec 03 '14 at 20:26
1

One way:

d = y.ravel()[0]
d.keys()

gives

['a', 'b']
cd98
  • 3,442
  • 2
  • 35
  • 51