简体   繁体   中英

Slice string in python

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!

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

use:

searchWords.extend([' '.join(words[i:]) for i in xrange(1, len(words))])
a = "All the best wishes"
[a.split(None,x)[-1] for x in xrange(1, len (a.split()))]

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']
s = "All the best wishes"
[' '.join(s.split()[x:]) for x in xrange(1, len(s.split()))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM