Срез строки в питоне - PullRequest
1 голос
/ 19 июля 2011

Я просто хочу нарезать строку с самого начала. Как будто у меня есть предложения:

"All the best wishes"

Я хочу получить

"the best wishes" , "best wishes", "wishes".

Любое решение, пожалуйста, спасибо!

Ответы [ 5 ]

5 голосов
/ 19 июля 2011
>>> words
['All', 'the', 'best', 'wishes']
>>> [" ".join(words[i:]) for i in range(len(words))]
['All the best wishes', 'the best wishes', 'best wishes', 'wishes']
>>> [" ".join(words[i:]) for i in range(len(words))][1:]
['the best wishes', 'best wishes', 'wishes']
4 голосов
/ 19 июля 2011

использование:

searchWords.extend([' '.join(words[i:]) for i in xrange(1, len(words))])
1 голос
/ 20 июля 2011
s = "All the best wishes"
[' '.join(s.split()[x:]) for x in xrange(1, len(s.split()))]
1 голос
/ 19 июля 2011

Э, питонеры;]

Вы всегда можете сделать это с помощью простого цикла и функции:

def parts(s, fromstart=True):
    sl, slp, idx = s.split(), [], 0 if fromstart else -1
    while len(sl)>1:
        sl.pop(idx)
        slp.append(' '.join(sl))
    return slp

s = 'All the best wishes'
parts(s) # -> ['the best wishes', 'best wishes', 'wishes']
parts(s,False) # -> ['All the best', 'All the', 'All']
1 голос
/ 19 июля 2011
a = "All the best wishes"
[a.split(None,x)[-1] for x in xrange(1, len (a.split()))]
...