开发者

String slicing in python

开发者 https://www.devze.com 2023-03-18 18:29 出处:网络
I want to slice word from the end.Suppose, I have some line with case sensitives(Upper/lower case) Abc Defg Hijk Lmn

I want to slice word from the end.Suppose, I have some line with case sensitives(Upper/lower case)

Abc Defg Hijk Lmn
Xyz Lmn jkf gkjhg

I want to slice them as like below :

Abc Defg Hijk
Abc Defg
Abc 

Then I need to take each sliced line in variables so that I can use them to search in some text file & return the whole text:

Suppose I have text :

 Akggf Abc Defg Hijk fgff jfkjgk djkfkgf     
 Akgff Abc fgff jfkjgk djkfkgf     
 Akggef Abc Defg  fgff jfk开发者_Python百科jgk djkfkgf
 gjshgs gskk Xyz Lmn jkf
 fgsgdf fkgksk Xyz Lmn

Any suggestions please.Thanks!


Use rsplit function:

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

and so on...

Another variant:

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


You can also use the following code:

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'

I have read your update and think that Roman's solution feats your needs. You can update your code the following way:

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

And if you need to get a full text:

resultText = '\n'.join(res)


To create a list from string:

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

look at it:

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

slice it, to remove last entry:

a[:-1]

This gives:

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

To join it into a string again:

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

gives:

'Abc Defg Hijk'

Now, repeat that in a loop...

0

精彩评论

暂无评论...
验证码 换一张
取 消