python中的切片字符串

python中的切片字符串,python,string,slice,Python,String,Slice,我只想从一开始就把字符串切分。就像我有一个句子: "All the best wishes" 我想去 "the best wishes" , "best wishes", "wishes". 任何解决方案请,谢谢 使用: searchWords.extend([' '.join(words[i:]) for i in xrange(1, len(words))]) 使用: 呃,蟒蛇;] 您始终可以通过简单的循环和函数来实现: def parts(s, fromstart=True):

我只想从一开始就把字符串切分。就像我有一个句子:

"All the best wishes"
我想去

"the best wishes" , "best wishes", "wishes".
任何解决方案请,谢谢

使用:

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

呃,蟒蛇;]

您始终可以通过简单的循环和函数来实现:

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']
呃,蟒蛇;]

您始终可以通过简单的循环和函数来实现:

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开始XRange或范围。请试着理解它是如何工作的,它不是火箭科学;您的意思是searchWords.extend[''.joinwords[:i]表示xrange-1中的i,lenwords]表示原始代码中的开始xrange或从1开始的范围。请试着理解它是如何工作的,它不是火箭科学;你是说searchWords.extend[''.joinwords[:i]表示xrange-1中的i,lenwords]
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()))]