5

输入 :

"The boy is running on the train"

预期输出:

["The boy", "boy is", "is running", "running on", "on the", "the train"]

在 python 中实现这一目标的最简单解决方案是什么。

4

4 回答 4

10
line="The boy is running on the train"
words=line.split()
k=[words[index]+' '+words[index+1] for index in xrange(len(words)-1)]
print k

输出

['The boy', 'boy is', 'is running', 'running on', 'on the', 'the train']
于 2013-08-31T09:04:02.123 回答
9

您拆分所有空间,然后重新加入对:

words = inputstr.split()
secondwords = iter(words)
next(secondwords)

output = [' '.join((first, second)) 
          for first, second in zip(words, secondwords)]

演示:

>>> inputstr = "The boy is running on the train"
>>> words = inputstr.split()
>>> secondwords = iter(words)
>>> next(secondwords)  # output is ignored
'The'
>>> [' '.join((first, second)) for first, second in zip(words, secondwords)]
['The boy', 'boy is', 'is running', 'running on', 'on the', 'the train']
于 2013-08-31T08:55:14.410 回答
3
import re

s = "The boy is running on the train"

print map(' '.join,re.findall('([^ \t]+)[ \t]+(?=([^ \t]+))',s))

编辑

Koustav Ghosal 的解决方案是最快的:

import re
from time import clock
from itertools import izip
from collections import defaultdict

s = "The boy is    running on the train"

z = 200
p = '%-9.6f %6.1f%%  %s'
rgx = re.compile('([^ \t]+)[ \t]+(?=([^ \t]+))')
R = defaultdict(list)

for rep in xrange(3000):

    t0 = clock()
    for i in xrange(z):
        map(' '.join,re.findall('([^ \t]+)[ \t]+(?=([^ \t]+))',s))
    te1 = clock()-t0
    R['e1'].append(te1)

    t0 = clock()
    for i in xrange(z):
        map(' '.join,rgx.findall(s))
    te2 = clock()-t0
    R['e2'].append(te2)

    t0 = clock()
    for i in xrange(z):
        words = s.split()
        secondwords = iter(words)
        next(secondwords)
        [' '.join((first, second))
         for first, second in zip(words, secondwords)]
    tM1 = clock()-t0
    R['M1'].append(tM1)

    t0 = clock()
    for i in xrange(z):
        words = s.split()
        secondwords = iter(words)
        next(secondwords)
        [' '.join((first, second))
         for first, second in izip(words, secondwords)]
    tM2 = clock()-t0
    R['M2'].append(tM2)

    t0 = clock()
    for i in xrange(z):
        words = s.split()
        secondwords = iter(words)
        next(secondwords)
        [' '.join(x)
         for x in izip(words, secondwords)]
    tM3 = clock()-t0
    R['M3'].append(tM3)

    t0 = clock()
    for i in xrange(z):
        words=s.split()
        [words[c]+' '+words[c+1] for c in range(len(words)-1)]
    tK1 = clock() - t0
    R['K1'].append(tK1)

    t0 = clock()
    for i in xrange(z):
        words=s.split()
        [words[c]+' '+words[c+1] for c in xrange(len(words)-1)]
    tK2 = clock() - t0
    R['K2'].append(tK2)

tmax = min(R['e1'])
for k,s in (('e1','eyquem with re.findall(pat,string)'),
            ('e2','eyquem with compiled_regex.findall(string)'),
            ('M1','Martijn Pieters'),
            ('M2','Martijn Pieters with izip'),
            ('M3','Martijn Pieters with izip and direct join'),
            ('K1','Koustav Ghosal'),
            ('K2','Koustav Ghosal with xrange')):
    t = min(R[k])
    print p % (t,t/tmax*100,s)

Python 2.7 的结果

0.007127   100.0%  eyquem with re.findall(pat,string)
0.004045    56.8%  eyquem with compiled_regex.findall(string)
0.003887    54.5%  Martijn Pieters
0.002522    35.4%  Martijn Pieters with izip
0.002152    30.2%  Martijn Pieters with izip and direct join
0.002030    28.5%  Koustav Ghosal
0.001856    26.0%  Koustav Ghosal with xrange
于 2013-08-31T09:54:24.227 回答
1

或者,一个解决方案itertools.combinations

>>> s = "The boy is running on the train"
>>> seen = set()
>>> new = []
>>> for tup in itertools.combinations(s.split(), 2):
...     if tup[0] not in seen:
...             new.append(' '.join(tup))
...             seen.add(tup[0])
... 
>>> print new
['The boy', 'boy is', 'is running', 'running on', 'on the', 'the train']

虽然这确实不是itertools.combinations应该用于 :p 的。

于 2013-08-31T08:59:33.950 回答