python - 在两个列表中查找匹配的子串

标签 python string pandas

我有两个列表:A 和 B。列表长度不相同,并且它们都包含字符串。匹配两个列表中的子字符串的最佳方法是什么?

list_A = ['hello','there','you','are']
list_B = ['say_hellaa','therefore','foursquare']

我想要一个名为 list_C 的匹配子字符串列表,其中包含:

list_C = ['hell','there','are']

我遇到了 this答案,但它要求我有一个匹配子串的列表。 有没有一种方法可以在不手动创建匹配子字符串列表的情况下获得我想要的内容?

This也无济于事,因为第二个列表包含子字符串。

最佳答案

这是一种方法。使用列表理解

list_A = ['hello','there','you','are']
list_B = ['hell','is','here']
jVal = "|".join(list_A)        # hello|there|you|are

print([i for i in list_B if i in jVal ])

输出:

['hell', 'here']

关于python - 在两个列表中查找匹配的子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085180/

相关文章:

python - Scikit-learn:如何获得真阳性、真阴性、假阳性和假阴性

python - tensorflow 值错误: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments

.net - vb.net:可以将一个字符串分割成一个字符串吗

python - 使用 Pandas Dataframe 制作调色板

Python Pandas - 重命名列后出现段错误?

python - 我的 Python 应用程序需要包含哪些文件?

javascript - 类型错误 : 'Undefined' object is not iterable?

c - strcpy 在 ios7 上表现不同

java - 在java中如何将带有整数的字符串转换为带有整数的数组?

python - 如何将函数应用于多个 Pandas 数据框