首先,我写了一个小例子:
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 array: 3036 list
https://drive.google.com/file/d/1FrggAa-JoxxoRqRs8NVV_F69DdVdiq_m/view?usp=sharing
numpy array: 300 list
给出了错误的答案。对于主文件,它必须给出3036布尔值,其中'True'只有300个数字。我不明白为什么这个答案是错误的??它似乎只适用于F的每个列表中的第3个字符。通过两个函数np,我们更倾向于使用reduce函数。In1d和np。Isin,而不是最后一个方法。如何解决以上三种方法??
让我知道这是否有效,
[x for x in map(lambda m: m in S, F)]
通过IM = [i in S for i in F]和IM = [x for x in map(lambda m: m in S, F)]代码将输入转换为列表,问题已经解决;然而,这些类型的转换在很小程度上降低了精度。如果这个问题可以用numpy函数来解决,比如np,那就很有用了。