我想把单词从头到尾切分。假设,我有一些区分大小写的行(大写/小写)

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

有什么建议吗?谢谢!

最佳答案

也可以使用以下代码:

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

或:
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'

我看过你的最新消息,认为罗曼的解决方案符合你的需要。您可以通过以下方式更新代码:
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)

关于python - python中的字符串切片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6623887/

10-14 16:06