python中的切片字符串

时间:2011-07-19 13:43:46

标签: python string slice

我只想从头开始切片。就像我有一句话:

"All the best wishes"

我想要

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

请解决任何问题,谢谢!

5 个答案:

答案 0 :(得分: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']

答案 1 :(得分:4)

使用:

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

答案 2 :(得分:1)

a = "All the best wishes"
[a.split(None,x)[-1] for x in xrange(1, len (a.split()))]

答案 3 :(得分:1)

呃,pythoners;]

您可以随时使用简单的循环和功能:

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

答案 4 :(得分:1)

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