2024-10-02 18:17:09 发布
网友
在没有for循环的情况下如何执行以下操作?你知道吗
import numpy as np l = np.array([[1, 3], 1, np.nan, [3, 53, 13], ['225gg2g']], dtype=object) loc = [1, 2] for i in loc: l[i] = ['wgwg', 23, 'g']
In [424]: l = np.array([[1, 3], 1, np.nan, [3, 53, 13], ['225gg2g']], dtype=object) In [425]: loc = [1,2] In [426]: l[loc] Out[426]: array([1, nan], dtype=object) In [427]: l[loc] = ['wgwg',23,'g'] - ValueError Traceback (most recent call last) <ipython-input-427-53c5987a9ac6> in <module>() > 1 l[loc] = ['wgwg',23,'g'] ValueError: cannot copy sequence with size 3 to array axis with dimension 2
它试图广播3项列表(转换为数组?)放进一个2个项目的插槽。你知道吗
我们可以将该列表包装到另一个列表中,因此它现在是单个项目列表:
In [428]: l[loc] = [['wgwg',23,'g']] In [429]: l Out[429]: array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']), list([3, 53, 13]), list(['225gg2g'])], dtype=object)
就像我们一个接一个地复制它,我们得到:
In [432]: l[loc[0]] = ['wgwg',23,'g'] In [433]: l[loc[1]] = ['wgwg',23,'g'] In [434]: l Out[434]: array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']), list([3, 53, 13]), list(['225gg2g'])], dtype=object)
解决3=>;2映射问题并非易事。你知道吗
但你为什么要这么做?我觉得很可疑。你知道吗
我先创建一个0d对象数组:
In [444]: item = np.empty([], object) In [445]: item.shape Out[445]: () In [446]: item[()] = ['wgwg',23,'g'] In [447]: item Out[447]: array(['wgwg', 23, 'g'], dtype=object) In [448]: l[loc] = item In [449]: l Out[449]: array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']), list([3, 53, 13]), list(['225gg2g'])], dtype=object)
它试图广播3项列表(转换为数组?)放进一个2个项目的插槽。你知道吗
我们可以将该列表包装到另一个列表中,因此它现在是单个项目列表:
就像我们一个接一个地复制它,我们得到:
解决3=>;2映射问题并非易事。你知道吗
但你为什么要这么做?我觉得很可疑。你知道吗
编辑
我先创建一个0d对象数组:
相关问题 更多 >
编程相关推荐