Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表到正则表达式,包括前导空格_Python_Python 3.x_Regex_Pandas - Fatal编程技术网

Python 列表到正则表达式,包括前导空格

Python 列表到正则表达式,包括前导空格,python,python-3.x,regex,pandas,Python,Python 3.x,Regex,Pandas,我的名单 要匹配文本中包含列表的内容,请执行以下操作 df text I love banana apple is delicious I eat pineapple hate whitegrape 匹配有一个问题,但由于列表前面没有空格,我要找的“苹果”包含在“菠萝”中,所以它匹配 作为另一个例子,我在寻找“葡萄”,但葡萄包含在白葡萄中,所以这也是计算 如何在列表中每个索引的开头留出一个空格 mylist = [f"(?i){re.escape(k.lower())}"

我的名单

要匹配文本中包含列表的内容,请执行以下操作

df

text
I love banana
apple is delicious
I eat pineapple
hate whitegrape

匹配有一个问题,但由于列表前面没有空格,我要找的“苹果”包含在“菠萝”中,所以它匹配

作为另一个例子,我在寻找“葡萄”,但葡萄包含在白葡萄中,所以这也是计算

如何在列表中每个索引的开头留出一个空格

mylist = [f"(?i){re.escape(k.lower())}" for k in mylist]
extracted = df['text'].str.lower().str.findall(f'({"|".join(mylist)})').apply(set)
df['matching'] = extracted.str.join(',')
结果是我想要的

result above
text                 matching
I love banana        banana
apple is delicious   apple
I eat pineapple      apple
hate whitegrape      grape
那你就可以分拆了

使用str.findall更新

那你就可以分拆了

使用str.findall更新

您可以使用:

df.text.str.lower().str.findall(r'\b({0})\b'.format('|'.join(mylist)))
Out[248]: 
0    [banana]
1     [apple]
2          []
3          []
Name: text, dtype: object
当然,您可以根据您的示例将extract更改为findall,您可以使用:

df.text.str.lower().str.findall(r'\b({0})\b'.format('|'.join(mylist)))
Out[248]: 
0    [banana]
1     [apple]
2          []
3          []
Name: text, dtype: object

当然,您可以根据您的示例将extract更改为findall

IEAT菠萝…Ilovebanana,对于这种情况,单词边界实际上是正则表达式中的两个可能会有所帮助:\b但有些文本无法识别。@YOBEN\S如果文本单词之间有空格,情况会一样吗?编辑contents@MichaelButscher即使在一个句子中的单词之间加了空格,也很难移动吗?请看上面修改的内容。IEAT菠萝…Ilovebanana,很难在这种情况下一个单词边界实际上在正则表达式中有两个可能会有帮助:\b但是一些文本不能用它们来识别。@YOBEN\S如果文本单词之间有空格,情况会一样吗?编辑contents@MichaelButscher即使在一个句子中的单词之间加了空格,也很难移动吗?请查看上面修改的内容。@ybin然后执行findallIt字符串可能有助于添加re.IGNORECASE标志,因为OP使用str.lower@尤本_S@MrNobody33是的,我使用了df['text'].str.lower.str.findallr'\b{0}\b'.format'|'.joinmylist.applysetSure@ybin,我知道,这只是一个建议,避免使用str.lower并使用re提供给我们的标志:@ybin然后执行字符串findallIt可能有助于添加re.IGNORECASE标志,因为OP使用str.lower@尤本_S@MrNobody33是的,我使用了df['text'].str.lower.str.findallr'\b{0}\b'.format'|'.joinmylist.applysetSure@ybin,我知道,这只是一个建议,避免使用str.lower并使用re提供给我们的标志:
df.text.str.lower().str.split().apply(lambda x : [y for y in x if y in mylist]).str[0]
Out[227]: 
0    banana
1     apple
2       NaN
3       NaN
Name: text, dtype: object
df.text.str.lower().str.findall(r'\b({0})\b'.format('|'.join(mylist)))
Out[248]: 
0    [banana]
1     [apple]
2          []
3          []
Name: text, dtype: object
 df.text.str.extract(f"(?i)\\b({'|'.join(mylist)})\\b")
        0
0  banana
1   apple
2     NaN
3     NaN