繁体   English   中英

检查另一个列表中的字符串列表中是否存在字符串字符?

[英]Checking the existence of string characters in list of strings in another list?

有没有比使用for循环更有效的方法来比较字符串列表?

我想检查y(在y字符串的任何部分)是否存在x字符串。

x = ['a1' , 'a2', 'bk']
y = ['a1aa' , 'a2lop' , 'bnkl', 'a1sss', 'flask']
for i in x:
    print([i in str_y for str_y in y])

结果:

[True, False, False, True, False]
[False, True, False, False, False]
[False, False, False, False, False]

使用列表压缩:

In [4]: [[b in a for a in y] for b in x]
Out[4]:
[[True, False, False, True, False],
 [False, True, False, False, False],
 [False, False, False, False, False]]

测试时间:

 %timeit print([[b in a for a in y] for b in x])
<lots of printing>
228 µs ± 5.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
 %timeit for i in x:   print([i in x for x in y])
<lots of printing>
492 µs ± 4.92 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

所以一半的时间。

您可以使用itertools.product将所有结果itertools.product在一个列表中:

In [61]: x = ['a1' , 'a2', 'bk']
    ...: y = ['a1aa' , 'a2lop' , 'bnkl', 'a1sss', 'flask']
    ...: 

In [62]: [i in j for i, j in product(x, y)]

或者作为一种功能方法,您可以将starmapproduct一起使用:

from itertools import product, starmap
from operator import contains

list((starmap(contains, product(y, x))))

同样,矢量化的BUT并不是非常优化,如下所示:

In [139]: (np.core.defchararray.find(y[:,None], x) != -1).T
Out[139]: 
array([[ True, False, False,  True, False],
       [False,  True, False, False, False],
       [False, False, False, False, False]])

您可以只使用list comprehension

x = ['a1' , 'a2', 'bk']
y = ['a1aa' , 'a2lop' , 'bnkl', 'a1sss', 'flask']
print([[xi in z for z in y] for xi in x])

不,我不这么认为,这不是一种直接方法,不需要大量的预计算。

如果您只需要知道needle s之一是否在haystack ,请使用any() -或使用haystackfor循环和break会更快:

needles = ['a1' , 'a2', 'bk']
haystacks = ['a1aa' , 'a2lop' , 'bnkl', 'a1sss', 'flask']

for haystack in haystacks:
    for needle in needles:
      if needle in haystack:
        print((needle, haystack))
        break  # Break if finding one match is enough

我认为唯一的解决方案是使用for循环...最好的办法是将代码保持在一行中:

print( [[xx in i for i in y] for xx in x] )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM