修改数组的值,其中相同大小的列表包含另一个列表的值

我有这两个清单:

List_large = ['a','b','c','d']
List_small = ['a','c']

和这个数组:

check = np.array([0]*len(List_large))
check
Out : array([0, 0, 0, 0])

我想在List_large位置中具有List_small值的数组“ check”中具有1。因此,我想最终拥有这个数组:

array([1, 0, 1, 0])

我该怎么办?

评论
  • wfuga
    wfuga 回复

    As a list-comprehension using a ternary operator:

    >>> List_large = ['a','b','c','d']
    >>> List_small = ['a','c']
    >>> np.array([1 if c in List_small else 0 for c in List_large])
    array([1, 0, 1, 0])
    
  • 柯
    回复

    You could use in1d method.

    result = np.in1d(List_large, List_small).astype(int)
    

    输出量

    array([1, 0, 1, 0])