4

I am currently attempting to modify a series of programs by utilizing dictionaries as opposed to arrays. I have columns of raw information in a file, which is then read into an ASCII csv file. I need to convert this file into a dictionary, so that it can be fed into another program.

I used a numpy.genfromtxt to pull out the information i need from the csv file, following this format:

a,b,c,d = np.genfromtxt("file",delimiter = ',', unpack = true)

this step works completely fine.

I then attempt to build a dictionary:

ouputDict = dict([a,a],[b,b],[c,c],[d,d])

As i understand it, this should make the key "a" in the dictionary a correspond to the array "a".

thus if:

a = [1,2,3,4]

then:

outputDict[a][0] = 1

However, when i attempt to create this dictionary i receive the following error:

TypeError: unhashable type: 'numpy.ndarray'

Why can't I construct an array in this fashion and what is the workaround, if any? Any help will be greatly appreciated!

7
  • trying to build a dict that maps an array to the same array makes very little sense. I think you may have to rethink what inputs the other program requires. It seems that a list of arrays may make more sense. Commented Nov 11, 2015 at 21:47
  • i understand that you can't map an array to itself, and i apologize for any confusion. i was trying to make the keys have the same name as the arrays for convenience's sake Commented Nov 11, 2015 at 21:49
  • 1
    Did you try: {'a':a, 'b':b,...}, or dict(a=a, b=b,...)? dict([('a', a), ('b', b)]) should also work. Commented Nov 11, 2015 at 21:49
  • 1
    I agree with what cel said, trying to implement dictionary (which is not necessary) where the problem can be solved with an array is not a good way to code. Commented Nov 11, 2015 at 21:50
  • The problem is right now it's trying to set a numpy array as a key at some point and this won't work because python doesn't allow mutable objects as a key in a dict. That's where the unhashable exception is coming from Commented Nov 11, 2015 at 21:51

2 Answers 2

5

You can do this even with using collections

Declare your dictionary as:

Dictionary = {}; // {} makes it a key, value pair dictionary

add your value for which you want an array as a key by declaring

Dictionary[a] = [1,2,3,4]; // [] makes it an array

So now your dictionary will look like

{a: [1,2,3,4]}

Which means for key a, you have an array and you can insert data in that which you can access like dictionary[a][0] which will give the value 1 and so on. :)

Btw.. If you look into examples of a dictionary, array and key value pairs, nested dictionary, your concept will get clearer.

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

2 Comments

Oh ya i get it now, well I just started explaining the basic format.. lol.. old habits
This was really helpful, I hadn't been exposed to storing arrays in dictionaries before! You've solved my problem, thank you for posting @AmanChawla
2

Copied from my comment:

Correct dictionary formats:

{'a':a, 'b':b,...}, or 
dict(a=a, b=b,...)
dict([('a', a), ('b', b),...])

The goal is to make the strings 'a','b',etc the keys, not the variable values.

1 Comment

Feel like a rookie, spent like an hour staring at dictionary documentation... thanks!

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.