In [88]: dict_created = {"A": [0,0], "B": [0,0], "C": [0,0], "D": [0,0],
...: "E": [0,0], "F": [0,0], "G": [0,0]}
...:
...: res_array = np.array(list(dict_created.items()))
<ipython-input-88-bc8f2c25c347>:4: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
In [89]: res_array
Out[89]:
array([['A', list([0, 0])],
['B', list([0, 0])],
['C', list([0, 0])],
['D', list([0, 0])],
['E', list([0, 0])],
['F', list([0, 0])],
['G', list([0, 0])]], dtype=object)
You have created an object dtype array containing letters and lists.
You weren't clear about the addition, but let's assume you did:
In [91]: res_array+list({"Q": [1,2]}.items())
<ipython-input-91-1bdaa5aac0d7>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
Out[91]:
array([['AQ', list([0, 0, 1, 2])],
['BQ', list([0, 0, 1, 2])],
['CQ', list([0, 0, 1, 2])],
['DQ', list([0, 0, 1, 2])],
['EQ', list([0, 0, 1, 2])],
['FQ', list([0, 0, 1, 2])],
['GQ', list([0, 0, 1, 2])]], dtype=object)
The first column is strings, and the +
is string concatenate.
The second is lists, which has a similar concatenate.
What you want array addition for the second column.
Let's turn that column into arrays:
In [93]: res_array[:,1]=[np.array(i) for i in res_array[:,1]]
In [94]:
In [94]: res_array
Out[94]:
array([['A', array([0, 0])],
['B', array([0, 0])],
['C', array([0, 0])],
['D', array([0, 0])],
['E', array([0, 0])],
['F', array([0, 0])],
['G', array([0, 0])]], dtype=object)
In [95]:
In [95]: res_array+list({"Q": np.array([1,2])}.items())
<ipython-input-95-2703d0356b14>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
Out[95]:
array([['AQ', array([1, 2])],
['BQ', array([1, 2])],
['CQ', array([1, 2])],
['DQ', array([1, 2])],
['EQ', array([1, 2])],
['FQ', array([1, 2])],
['GQ', array([1, 2])]], dtype=object)
I'm leaving the 'ragged array' warning that np version 1.19 started adding. You should be aware that you are creating a non-standard numpy
array, and treat it appropriately. Making a numpy array from a dict
, especially if you want both keys and values is a bit odd. numpy
is best for arrays with uniform dtype - all character or all numbers. This mixed stuff is awkward.
Here is a non-numpy way of making a new dict:
In [97]: newdict= {key+'Q': [i+j for i,j in zip(value,[1,2])] for key, value in dict_created.items()}
In [98]: newdict
Out[98]:
{'AQ': [1, 2],
'BQ': [1, 2],
'CQ': [1, 2],
'DQ': [1, 2],
'EQ': [1, 2],
'FQ': [1, 2],
'GQ': [1, 2]}