Python Appending a dictionary whose keys contain np arrays

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Arrays
AI Thread Summary
The discussion revolves around issues encountered while trying to append a new numpy array to an existing key in a dictionary that stores numpy arrays. The user attempts to use the `setdefault` method with a list as the default value, leading to confusion and errors. The first error arises from trying to use `np.append` incorrectly, as 'np' is not an attribute of numpy arrays. The second error occurs because the append method is being called on a numpy array, which does not support this operation. The user later clarifies that they converted the numpy array to a standard Python list using `np_data.tolist()`, which resolved the issue. The conversation highlights the importance of understanding the differences between numpy arrays and Python lists, and the correct usage of numpy functions.
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 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 member 428835
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top