我的名单
mylist = [apple, banana, grape]
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
结果是我想要的
text matching
I love banana banana
apple is delicious apple
I eat pineapple
hate whitegrape
然后你可以做
split
更新为
str.findall
您可以使用:
当然,您可以根据您的示例将
extract
更改为findall
相关问题 更多 >
编程相关推荐