2

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 jfkjgk djkfkgf
 gjshgs gskk Xyz Lmn jkf
 fgsgdf fkgksk Xyz Lmn

Any suggestions please.Thanks!

Big.Bang
  • 33
  • 5

3 Answers3

5

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']
Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
1

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)
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
  • @ Artsiom Rudzenka : Could you please a bit elaborate.I mean how to get the desired sliced line for many dataStr.Thanks! – Big.Bang Jul 08 '11 at 12:02
  • Give me an example so i will be able to help you. – Artsiom Rudzenka Jul 08 '11 at 12:05
  • @ Artsiom Rudzenka : I have updated the question.Could you please have a look! – Big.Bang Jul 08 '11 at 12:17
  • Could you please have a look over this part `searchWords.extend([' '.join(words[:i]) for i in xrange(len(words), 0, -1)])` – Big.Bang Jul 08 '11 at 13:33
  • What is wrong with it? I have copied all code and rerun it - all is ok – Artsiom Rudzenka Jul 08 '11 at 13:40
  • If I print `searchWords` get like two times everything i.e. `['Abc Defg Hijk Lmn', 'Abc Defg Hijk', 'Abc Defg', 'Abc', 'Abc Defg Hijk Lmn', 'Abc Defg Hijk', 'Abc Defg', 'Abc'] ['Abc Defg Hijk Lmn', 'Abc Defg Hijk', 'Abc Defg', 'Abc', 'Abc Defg Hijk Lmn', 'Abc Defg Hijk', 'Abc Defg', 'Abc', 'Xyz Lmn jkf gkjhg', 'Xyz Lmn jkf', 'Xyz Lmn', 'Xyz']` – Big.Bang Jul 08 '11 at 13:42
  • You can contact me using skype( you can find it within linked profile in my info) – Artsiom Rudzenka Jul 08 '11 at 13:44
  • I can't update code till you give me sample of your input, i have checked my code twice in console and pycharm and it gives me the very same result. – Artsiom Rudzenka Jul 08 '11 at 18:23
0

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...

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Note that `s != " ".join(s.split())` while `s == " ".join(s.split(" "))` when `s = "Two spaces"` – phant0m Jul 08 '11 at 11:51