无法将数组追加到列表中

所以我想运行此代码来查找包含带有特定条件的numpy数组的列表的随机元素

The conditions are that is for every element of arr1 not equal to arr2 it will append the element of that index of er and append it to lst:

import numpy as np
import random
arr1 = np.array([1,2,3,4])
arr2 = np.array([1,2,6,3])
arr = (arr1 == arr2)
er = np.array([[1,2],[-6,7],[4,7],[6,2]])

lst = []
for i in arr:
    if i == False:
        lst.append(er[i])

print(random.choice(lst))

但我不知道为什么它返回一个空列表。请帮助

评论
  • C 咯
    C 咯 回复

    arr是一个布尔数组,因此在for循环中,我是True / False值(不是索引)

    for i in arr:
    

    更改

    for i in arr:
        if i == False:
            lst.append(er[i])
    

    至:

    for i, v in enumerate(arr):   # retrieving index and value
        if v == False:            # test value
            lst.append(er[i])     # use i to index into er