np.where不更改给定列表的numpy数组

2024-09-30 01:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想有条件地填充一个给定列表的numpy数组

遍历my_listnumpy数组的每个元素并查找dup_list以查看它是否有pair值,例如,1的pair值为35。然后,将my_list中的1更改为35

my_list = np.array([1, 2, 3, 4])
dup_list = [[1, 35], [4, 31]]

my_list的理想结果:

array([35, 2, 3, 31])

我下面的代码没有改变任何东西

for dup in dup_list:
    np.where(my_list==dup[0], dup[1], my_list) 

my_list

array([1, 2, 3, 4])

Tags: 代码numpy元素列表formynp数组

热门问题