Appending a dictionary whose keys contain np arrays

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to appending data to a dictionary where the keys are associated with numpy arrays. Participants are exploring the correct methods to append new data to an existing numpy array stored in a dictionary, addressing errors encountered during the process.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to append a numpy array to a dictionary key using the method data_dict.setdefault(key_curr, []).np.append(data_col_curr), which results in an AttributeError.
  • Another participant points out confusion regarding the presence of "np" in the code and questions the use of a Python list as the default value in the setdefault call, given that the dictionary is supposed to store numpy arrays.
  • A participant acknowledges a correction in their code and mentions converting the numpy array to a standard list using np_data.tolist() as a solution to the issue.
  • There is a clarification that "np" is not an attribute of numpy arrays, which explains the error encountered in the first line of code.
  • One participant notes that using a standard list instead of a numpy array is acceptable as long as the capabilities of numpy arrays are not required.

Areas of Agreement / Disagreement

Participants express confusion and seek clarification on the errors encountered, but there is no consensus on the best approach to resolve the issue. Multiple viewpoints on the use of lists versus numpy arrays are presented.

Contextual Notes

There are unresolved questions regarding the compatibility of data types being used (numpy arrays vs. Python lists) and the implications of using different data structures in this context.

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
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   Reactions: member 428835
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
 
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!
 
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   Reactions: member 428835

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
1K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
22
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K