字符串切片在python中

时间:2011-07-08 11:41:18

标签: python substring slice

我想从最后切片。假设,我有一些区分大小写(大/小写)

Abc Defg Hijk Lmn
Xyz Lmn jkf gkjhg

我想将它们切成如下:

Abc Defg Hijk
Abc Defg
Abc 

然后我需要将每个切片线放在变量中,以便我可以使用它们来搜索某些文本文件&返回全文:

假设我有文字:

 Akggf Abc Defg Hijk fgff jfkjgk djkfkgf     
 Akgff Abc fgff jfkjgk djkfkgf     
 Akggef Abc Defg  fgff jfkjgk djkfkgf
 gjshgs gskk Xyz Lmn jkf
 fgsgdf fkgksk Xyz Lmn

请提出任何建议。谢谢!

3 个答案:

答案 0 :(得分:5)

使用rsplit功能:

>>> s = 'Abc Defg Hijk Lmn'
>>> s.rsplit(' ', 1)[0]
'Abc Defg Hijk'
>>> s = s.rsplit(' ', 1)[0]
>>> s.rsplit(' ', 1)[0]
'Abc Defg'

依旧......

另一种变体:

>>> words = s.split()
>>> [' '.join(words[:i]) for i in range(len(words), 0, -1)]
['Abc Defg Hijk Lmn', 'Abc Defg Hijk', 'Abc Defg', 'Abc']

答案 1 :(得分:1)

您还可以使用以下代码:

dataStr = 'Abc Defg Hijk Lmn'
for word in reversed(dataStr.split()):
    # do something with word

OR:

dataStr = 'Abc Defg Hijk Lmn'
removeLastWord = lambda line: ' '.join([word for word in line.split()[:-1]])
dataStr = removeLastWord(dataStr)
>>> 'Abc Defg Hijk'
dataStr = removeLastWord(dataStr)
>>> 'Abc Defg'
dataStr = removeLastWord(dataStr)
>>> 'Abc'

我已阅读您的更新,并认为Roman的解决方案可满足您的需求。您可以通过以下方式更新代码:

searchTxt = """Abc Defg Hijk Lmn
Xyz Lmn jkf gkjhg"""

data = """kggf **Abc Defg Hijk** fgff jfkjgk djkfkgf
 Akggf **Abc ** fgff jfkjgk djkfkgf
 Akggf **Abc Defg  fgff jfkjgk djkfkgf
 gjshgs gskk **Xyz Lmn jkf**
 fgsgdf fkgksk **Xyz Lmn**"""

searchWords = []
for line in (line for line in searchTxt.split('\n') if line.strip()):
    words = line.split()
    searchWords.extend([' '.join(words[:i]) for i in xrange(len(words), 0, -1)])

searchWords = sorted(searchWords, key=len, reverse=True)# to look first for the longest string match

res = set([line for sword in searchWords for line in data.split('\n') if sword in line])

# OR

res = []
for line in data.split('\n'):
    for sword in searchWords:
        if sword in line:
            res.append(line)
            break

如果你需要获得全文:

resultText = '\n'.join(res)

答案 2 :(得分:0)

从字符串创建列表:

a="Abc Defg Hijk Lmn".split()

看看它:

['Abc', 'Defg', 'Hijk', 'Lmn']

将其切片,删除最后一个条目:

a[:-1]

这给出了:

['Abc', 'Defg', 'Hijk']

再次将其加入字符串:

" ".join(a[:-1])

给出:

'Abc Defg Hijk'

现在,在循环中重复一遍......