1

The desired result is either a function or a way to find where is a sentence within a list of strings.

sentence = 'The cat went to the pool yesterday'

structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.']

for example

def findsentence(sentence, list of strings):
      # do something to get the output, vec of positions to find the sentence in hte string list
return output

findsentence(sentence, structure)
> (0,1) # beacuse the phrase is splitted in the list...

Caution!!

The challenge it is not to find exactly the sentence. Look at the example, this sentence is part of sentence position 0 and part in structure postition 1.

So this is not a simple, string manipulation problem.

PeCaDe
  • 277
  • 1
  • 8
  • 33

3 Answers3

3

Use the following :

sentence = "foo sam bar go"
structure = ["rq", "foo sam", "bar go", "ca", "da"]

def findsentencelist(sentence, list_of_strings):
    l = []
    for item in list_of_strings:
        if item in sentence:
            l.append(list_of_strings.index(item))
    return l

print str(findsentencelist(sentence, structure))

Hopefully this will help you, Yahli.

EDIT :

There is a problem with your variables. Your sentence MUST be a string - not a list. Edit your variables and try this function again :)

SECOND EDIT: I think I've finally understood what you're trying to do. Let me know if this one works better.

THIRD EDIT: Jesus, Hopefully this one would solve your problem. Let me know if it did the trick :)

Mr Yahli
  • 321
  • 2
  • 13
  • Not working... returning None – PeCaDe May 09 '17 at 12:06
  • 1
    It is. Just make sure that the sentence must be from a string type and not a list! The correct way to set the variable is this : sentence = 'When I think about what you are doing'. – Mr Yahli May 09 '17 at 12:11
  • 1
    @PeCaDe, I've edited my answer accordingly. Please try it again and let me know if it worked for you! – Mr Yahli May 09 '17 at 12:16
  • the challegne is not to find exaclty the sentence you have tried. my example and question was with this one, sentence = 'When I think about what you are doing, I wonder if you realize the effect you are having on me', is composed with two srings from the string list! So, please caution with not to read properly the questions. – PeCaDe May 09 '17 at 12:32
  • @PeCaDe, I've re-edited my answer. Please try it :) – Mr Yahli May 09 '17 at 12:53
  • the correct scenary would be. sentence = "foo bar" structure = ["sa", "foo", "bar", "ca", "da"]. And you find that your sentence is in the structure index 0 and 1. – PeCaDe May 09 '17 at 12:57
  • 1
    @PeCaDe, Try it now. Please next time make sure you explain your question better :) – Mr Yahli May 09 '17 at 13:10
  • 1
    Thnks so much that was the right answer, I couldnt express my self properly. thx – PeCaDe May 09 '17 at 14:51
  • 1
    I'm glad I could help! – Mr Yahli May 09 '17 at 17:42
2

I just remove punctuations on structure to make it work:

sentence = 'The cat went to the pool yesterday'

structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.','Life is too short as it is. In short, she had a cushion job.']

import string

def findsentence(sentence, list_of_strings):
    return tuple(i for i, s in enumerate(list_of_strings) if s.translate(None, string.punctuation) in sentence)


print findsentence(sentence, structure)
# (0, 1)
Kruupös
  • 5,097
  • 3
  • 27
  • 43
0

After removing the punctuation. You can use this code to get the index ,

for i,j in enumerate(structure):
     if j in sentence:
          print(i) 

Hope this solves your problems. There are quite other solutions as python is flexible.

mohan08p
  • 5,002
  • 1
  • 28
  • 36