【发布时间】:2020-10-01 02:02:16
【问题描述】:
有很多类似的问题都有相同的解决方案:我如何检查我的字符串列表和更大的字符串,看看是否有匹配项? How to check if a string contains an element from a list in PythonHow to check if a line has one of the strings in a list?
我有一个不同的问题:我如何检查我的字符串列表和一个更大的字符串,看看是否有匹配,并隔离字符串,以便我可以执行另一个与匹配字符串相关的字符串操作?
以下是一些示例数据:
| id | data |
|--------|---------------------|
| 123131 | Bear Cat Apple Dog |
| 123131 | Cat Ap.ple Mouse |
| 231321 | Ap ple Bear |
| 231321 | Mouse Ap ple Dog |
最终,我试图找到“apple”['Apple', 'Ap.ple', 'Ap ple'] 的所有实例,虽然匹配哪个并不重要,但我需要能够找出是 Cat 还是 Bear存在于它之前或之后。匹配字符串的位置无关紧要,只是能够确定它之前或之后的内容。
在Bear Cat Apple Dog 中,熊在苹果之前,尽管猫在路上。
这是我的示例代码所在的位置:
data = [[123131, "Bear Cat Apple Dog"], ['123131', "Cat Ap.ple Mouse"], ['231321', "Ap ple Bear"], ['231321', "Mouse Ap ple Dog"]]
df = pd.DataFrame(data, columns = ['id', 'data'])
def matching_function(m):
matching_strings = ['Apple', 'Ap.ple', 'Ap ple']
if any(x in m for x in matching_strings):
# do something to print the matched string
return True
df["matched"] = df['data'].apply(matching_function)
在正则表达式中这样做会更好吗?
现在,该函数只返回 true。但是,如果有匹配项,我想它也可以返回 matched_bear_before matched_bear_after 或 Cat 的相同值并将其填充到 df['matched'] 列中。
这是一些示例输出:
| id | data | matched |
|--------|---------------------|---------|
| 123131 | Bear Cat Apple Dog | TRUE |
| 123131 | Cat Ap.ple Mouse | TRUE |
| 231321 | Ap ple Bear | TRUE |
| 231321 | Mouse Ap ple Dog | FALSE |
【问题讨论】:
-
所以你想知道其中一个字符串出现在文本中,并且你想在匹配字符串之前和之后提取单词吗?
-
我会使用正则表达式 - 你可以一口气在苹果旁边测试 cat 和 bear。
-
是的,我想知道其中一个字符串是否连续出现,先查看之前是否存在Bear或Cat,然后查看之后是否存在
-
@kabaname in "Bear Cat Apple Dog" - 答案是什么?
-
我已经调整了示例数据和示例输出,以反映我要寻找的内容,对于初学者来说。如果之前和/或之后有匹配,该函数只需返回 true。然而,我正在寻找的关键能力是能够简单地识别一个字符串并在它之前或之后寻找一些东西。
标签: python python-3.x regex pandas