>>> 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
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
输出:
^{pr2}$分离:
首先,根据
::
进行拆分,并对连续匹配进行压缩分组{cd2>如果你得到这个表达式:
接下来,应用一个函数从每个元组的第一个元素提取最后一个单词,从第二个元素提取第一个单词:
如果您
list()
该表达式,您将得到:最后,将每个元组连接到列表理解中:
输出:
^{pr2}$timeit
结果:这是
re
的另一种方法。在输出:
^{pr2}$timeit
结果:另一种方法是:
1)获取分隔符的索引
第二)将分隔符周围的单词连接起来
^{pr2}$使用
re.findall()
函数的解决方案:输出:
^{pr2}$相关问题 更多 >
编程相关推荐