我可以使用re.finditer
轻松提取文本中字符串的开始/结束位置,从而得出匹配项开始/结束位置的元组。
s1 = 'the quick quick brown fox jumps over the lazy dog'
s2 = 'Pack my box with five dozen liquor jugs'
s3 = 'How razorback jumping frogs can level six piqued gymnasts'
r1 = '(quick|fox|dog)'
r2 = '(box|five|jugs)'
r3 = '(frogs|six)'
t = [(s1,r1), (s2,r2), (s3,r3)]
for e in t:
print([(f.start(), f.end()) for f in re.finditer(e[1],e[0])])
[(4, 9), (10, 15), (22, 25), (46, 49)]
[(8, 11), (17, 21), (35, 39)]
[(22, 27), (38, 41)]
我有一个DataFrame,其中一列为文本,而正则表达式为另一列
s = pd.DataFrame(data={'re':[r1,r2,r3], 'text':[s1,s2,s3]})
re text
0 (quick|fox|dog) the quick quick brown fox jumps over the lazy dog
1 (box|five|jugs) Pack my box with five dozen liquor jugs
2 (frogs|six) How razorback jumping frogs can level six piqu...
我想使用Series
方法将相同的位置信息提取到pandas.str
对象中,但是没有finditer
方法(在熊猫0.23中)。
是否有一种方法可以不借助for-each循环?
答案 0 :(得分:1)
扩展@ user3483203的注释,您可以使用list comprehension执行以下操作:
import re
import pandas as pd
s1 = 'the quick quick brown fox jumps over the lazy dog'
s2 = 'Pack my box with five dozen liquor jugs'
s3 = 'How razorback jumping frogs can level six piqued gymnasts'
r1 = '(quick|fox|dog)'
r2 = '(box|five|jugs)'
r3 = '(frogs|six)'
t = [(s1,r1), (s2,r2), (s3,r3)]
s = pd.DataFrame(data={'re':[r1,r2,r3], 'text':[s1,s2,s3]})
result = pd.Series([[(f.start(), f.end()) for f in re.finditer(p, s)] for p, s in zip(s.re, s.text)])
print(result)
输出
0 [(4, 9), (10, 15), (22, 25), (46, 49)]
1 [(8, 11), (17, 21), (35, 39)]
2 [(22, 27), (38, 41)]
dtype: object
另一种替代方法是使用apply,但我认为这可能会更慢:
def finditer(p, s):
return [(f.start(), f.end()) for f in re.finditer(p, s)]
result = s[['re', 'text']].apply(lambda x: finditer(x[0], x[1]), axis=1)
print(result)