查找delimi前后的单词

2024-09-30 10:36:02 发布

您现在位置:Python中文网/ 问答频道 /正文

string = "The is a better :: sentence as :: compared to that" 

输出:

  1. 更好的判决
  2. 相比之下

我试过以下方法

^{pr2}$

这些不会给我具体的词


Tags: theto方法stringthatisassentence
3条回答
>>> string = "The is a better :: sentence as :: compared to that" 
>>> x = [' '.join(x) for x in map(lambda x: (x[0].split()[-1], x[1].split()[0]), zip(string.split('::')[:-1], string.split('::')[1:]))]
>>> x

输出:

^{pr2}$

分离:

首先,根据::进行拆分,并对连续匹配进行压缩分组

pairs = zip(string.split('::')[:-1], string.split('::')[1:]))

{cd2>如果你得到这个表达式:

[('The is a better ', ' sentence as '), (' sentence as ', ' compared to that')]

接下来,应用一个函数从每个元组的第一个元素提取最后一个单词,从第二个元素提取第一个单词:

new_pairs = map(lambda x: (x[0].split()[-1], x[1].split()[0]), pairs)

如果您list()该表达式,您将得到:

[('better', 'sentence'), ('as', 'compared')]

最后,将每个元组连接到列表理解中:

result = [' '.join(x) for x in new_pairs]

输出:

^{pr2}$

timeit结果:

The slowest run took 4.92 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.74 µs per loop

这是re的另一种方法。在

import re
string = "The is a better :: sentence as :: compared to that" 
result = [' '.join(x) for x in re.findall('([\w]+) :: ([\w]+)', string)]

输出:

^{pr2}$

timeit结果:

The slowest run took 4.60 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.49 µs per loop

另一种方法是:

1)获取分隔符的索引

indices = [idx for idx, elem in enumerate(string.split(' ')) if elem == '::']

第二)将分隔符周围的单词连接起来

^{pr2}$

使用re.findall()函数的解决方案:

s = "The is a better :: sentence as :: compared to that"
result = [' '.join(i) for i in re.findall(r'(\w+) ?:: ?(\w+)', s)]

print(result)

输出:

^{pr2}$

相关问题 更多 >

    热门问题