用第二个空格拆分字符串

时间:2013-08-31 08:53:41

标签: python string split

输入:

"The boy is running on the train"

预期输出:

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

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

4 个答案:

答案 0 :(得分:9)

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']

答案 1 :(得分:8)

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

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']

答案 2 :(得分: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

答案 3 :(得分: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。