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)
您已经创建了一个包含字母和列表的对象 dtype 数组。
您不清楚添加内容,但假设您做了:
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)
第一列是字符串,+是字符串拼接。
第二个是列表,有类似的连接。
您想要为第二列添加数组。
让我们把那一列变成数组:
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)
我将留下 np 1.19 版开始添加的“参差不齐的数组”警告。您应该知道您正在创建一个非标准的numpy 数组,并对其进行适当的处理。从 dict 创建一个 numpy 数组,特别是如果你想要键和值都有点奇怪。 numpy 最适合具有统一 dtype 的数组 - 所有字符或所有数字。这种混杂的东西很尴尬。
这是一种制作新字典的非 numpy 方式:
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]}