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!

2
  • so those selections are the first 3, 2, and 1, words of the line? Commented Jul 8, 2011 at 11:45
  • @Dan D.:Yes, it is. But I need to do this process globally & take each sliced line in varriables. Commented Jul 8, 2011 at 11:48

3 Answers 3

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']
Sign up to request clarification or add additional context in comments.

1 Comment

[' '.join(words[:i]) for i in range(len(words), 0, -1)] Like it! Many thanks.
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)

8 Comments

@ Artsiom Rudzenka : Could you please a bit elaborate.I mean how to get the desired sliced line for many dataStr.Thanks!
Give me an example so i will be able to help you.
@ Artsiom Rudzenka : I have updated the question.Could you please have a look!
Could you please have a look over this part searchWords.extend([' '.join(words[:i]) for i in xrange(len(words), 0, -1)])
What is wrong with it? I have copied all code and rerun it - all is ok
|
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...

1 Comment

Note that s != " ".join(s.split()) while s == " ".join(s.split(" ")) when s = "Two spaces"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.