Appending a dictionary whose keys contain np arrays

  • Python
  • Thread starter member 428835
  • Start date
  • Tags
    Arrays
In summary, the conversation discusses an error that occurs when trying to append a key and value to a dictionary that stores data in numpy arrays. The first attempt, using the "np" attribute, results in an AttributeError, while the second attempt, without the "np" attribute, results in a different AttributeError. The conversation also mentions the use of a Python list as the default value in the "setdefault" call and the use of "np" at the beginning of the python file. The final solution involves converting the np array to a standard array using the "tolist()" function.
  • #1
member 428835
Hi PF!

I have a large dataset called data_dict I'm parsing that's stored as a numpy array. I'm subcategorizing it via a dictionary titled data_dict. Currently there is a dictionary key called key_curr that stores an np array data_col_prev. I'd like to append to this key data_col_curr. When I execute the following:

Python:
data_dict.setdefault(key_curr, []).np.append(data_col_curr)

I receive the error
>> AttributeError: 'numpy.ndarray' object has no attribute 'np'

When I execute

Python:
data_dict.setdefault(key_curr, []).append(data_col_curr)

I receive the error
>> AttributeError: 'numpy.ndarray' object has no attribute 'append'

Any help would be awesome!
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
It seems like you are saying that executing the same statement gives you two different errors. That doesn't make sense.

That said, I don't understand why the "np" is in the statement you are executing. I also don't understand why the default value in the "setdefault" call is a Python list, but you say the values stored in the dict are np arrays.
 
  • Like
Likes member 428835
  • #3
PeterDonis said:
It seems like you are saying that executing the same statement gives you two different errors. That doesn't make sense.

That said, I don't understand why the "np" is in the statement you are executing. I also don't understand why the default value in the "setdefault" call is a Python list, but you say the values stored in the dict are np arrays.
Sorry, I changed the second line of code, which I now think makes more since. And sorry, np comes from the beginnning of the python file: import numpy as np
 
  • #4
PeterDonis said:
It seems like you are saying that executing the same statement gives you two different errors. That doesn't make sense.

That said, I don't understand why the "np" is in the statement you are executing. I also don't understand why the default value in the "setdefault" call is a Python list, but you say the values stored in the dict are np arrays.
Actually, your comment regarding the [] not compatible with numpy led to me getting a solution (I changed the np array to standard array via np_data.tolist() )

Thanks!
 
  • #5
joshmccraney said:
I changed the second line of code
Yes, I see that.

joshmccraney said:
np comes from the beginnning of the python file: import numpy as np
That doesn't make "np" an attribute of arrays. So that explains the error you are getting from the first line of code.

joshmccraney said:
I changed the np array to standard array via np_data.tolist()
Actually it's a Python list object, not a Python array object. Python array objects are different.

As long as you don't need any of the capabilities of the np array object, this should work fine.
 
  • Like
Likes member 428835

FAQ: Appending a dictionary whose keys contain np arrays

1. What does it mean to append a dictionary?

Appending a dictionary means adding new key-value pairs to an existing dictionary.

2. What are np arrays in the context of dictionaries?

np arrays are multidimensional arrays from the NumPy library that can store and manipulate large amounts of data efficiently.

3. How do you append a dictionary whose keys contain np arrays?

To append a dictionary with np array keys, you can use the dictionary's update() method or the square bracket notation to add new key-value pairs.

4. Can you append a dictionary with np array keys to another dictionary?

Yes, you can append a dictionary with np array keys to another dictionary by using the update() method or the square bracket notation.

5. What are some potential uses for appending a dictionary with np array keys?

Appending a dictionary with np array keys can be useful in scientific data analysis and machine learning applications where large amounts of data need to be stored and manipulated efficiently. It can also be used for creating complex data structures and organizing data in a hierarchical manner.

Similar threads

Replies
3
Views
1K
Replies
29
Views
2K
Replies
8
Views
1K
Replies
9
Views
2K
Replies
3
Views
2K
Replies
5
Views
3K
Replies
4
Views
7K
Replies
3
Views
2K
Back
Top