1

I just want to slice string from the beginning.As like I have a sentences:

"All the best wishes"

I want to get

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

Any solution please,Thanks!

5 Answers 5

5
>>> 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']
Sign up to request clarification or add additional context in comments.

Comments

4

use:

searchWords.extend([' '.join(words[i:]) for i in xrange(1, len(words))])

2 Comments

in the original code start xrange(or range) from 1. please try to understand how it works, it's not rocket science ;)
You mean searchWords.extend([' '.join(words[:i]) for i in xrange(-1, len(words))])
1
a = "All the best wishes"
[a.split(None,x)[-1] for x in xrange(1, len (a.split()))]

Comments

1

Eh, pythoners;]

You can always do it with simple loop and function:

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

Comments

1
s = "All the best wishes"
[' '.join(s.split()[x:]) for x in xrange(1, len(s.split()))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.