【发布时间】:2021-06-11 20:14:07
【问题描述】:
首先,我写了一个 to list 的小例子:
F = [[1,2,3],[3,2,7],[4,4,1],[5,6,3],[1,3,7]] # (1*5) 5 lists
S = [[1,3,7],[6,8,1],[3,2,7]] # (1*3) 3 lists
我想在两个 F 和 S 中获取相同“列表”的布尔矩阵:
[False, True, False, False, True] # (1*5) 5 Booleans for 5 lists of F
通过使用IM = reduce(np.in1d, (F, S)),它会为每个 F 列表中的每个数字提供结果:
[ True True True True True True False False True False True True
True True True] # (1*15)
通过使用IM = reduce(np.isin, (F, S)),它也会为每个 F 列表中的每个数字提供结果,但形状不同:
[[ True True True]
[ True True True]
[False False True]
[False True True]
[ True True True]] # (5*3)
示例列表的代码IM = [i in S for i in F] 将实现真正的结果,但是当我将此代码用于我的两个主要的更大的 numpy 列表数组时:
https://drive.google.com/file/d/1YUUdqxRu__9-fhE1542xqei-rjB3HOxX/view?usp=sharing
numpy 数组:3036 个列表
https://drive.google.com/file/d/1FrggAa-JoxxoRqRs8NVV_F69DdVdiq_m/view?usp=sharing
numpy 数组:300 个列表
它给出了错误的答案。对于主文件,它必须给出 3036 布尔值,其中“真”只有 300 个数字。我不明白为什么会得到错误的答案??似乎它仅适用于每个 F 列表中的第三个字符。 最好通过 np.in1d 和 np.isin 这两个函数来使用 reduce 函数,而不是最后一种方法。上述三种方法如何分别解决??
【问题讨论】:
标签: python list numpy scipy numpy-ndarray