0

I'm trying to create a basic program to pick out the positions of words in a quote. So far, I've got the following code:

print("Your word appears in your quote at position(s)", string.index(word))

However, this only prints the first position where the word is indexed, which is fine if the quote only contains the word once, but if the word appears multiple times, it will still only print the first position and none of the others.

How can I make it so that the program will print every position in succession?

Note: very confusingly, string here stores a list. The program is supposed to find the positions of words stored within this list.

amezkilljoy
  • 3
  • 1
  • 4
  • What is the output you expect, what does `string` contain? – pp_ Feb 20 '16 at 19:18
  • 1
    Possible duplicate of [How to find all occurrences of an element in a list?](http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – idjaw Feb 20 '16 at 19:18
  • @pp_ the string is the name of the list I used to store the user's quote in (slightly confusing, sorry) – amezkilljoy Feb 20 '16 at 19:21
  • I am still confused. – pp_ Feb 20 '16 at 19:22
  • @idjaw I did look at that before I posted the question but none of the solutions seemed to work – amezkilljoy Feb 20 '16 at 19:22
  • 1
    Did you also look here: http://stackoverflow.com/questions/3873361/finding-multiple-occurrences-of-a-string-within-a-string-in-python ? – Maecky Feb 20 '16 at 19:23
  • 1
    The link by @idjaw is correct for lists. For finding substrings in a string, this is a duplicate of http://stackoverflow.com/questions/4664850/find-all-occurrences-of-a-substring-in-python – James Feb 20 '16 at 19:26

3 Answers3

1

It seems that you're trying to find occurrences of a word inside a string: the re library has a function called finditer that is ideal for this purpose. We can use this along with a list comprehension to make a list of the indexes of a word:

>>> import re
>>> word = "foo"
>>> string = "Bar foo lorem foo ipsum"
>>> [x.start() for x in re.finditer(word, string)]
[4, 14]

This function will find matches even if the word is inside another, like this:

>>> [x.start() for x in re.finditer("foo", "Lorem ipsum foobar")]
[12]

If you don't want this, encase your word inside a regular expression like this:

[x.start() for x in re.finditer("\s+" + word + "\s+", string)]
Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78
  • Hmm, your answer is better than mine. Should I remove my answer then? (Pretty new to stack overflow) – Amit Gold Feb 20 '16 at 19:38
  • @AmitGold, your answer is fine, keep it; some people viewing this question later might not want to use a library like I have. You've missed a `]` off the end of your list comprehension though. – Aaron Christiansen Feb 20 '16 at 19:39
0

Here is a code I quickly made that should work:

string = "Hello my name is Amit and I'm answering your question".split(' ')
indices = [index for (word, index) in enumerate(string) if word == "QUERY"]

That should work, although returns the index of the word. You could make a calculation that adds the lengths of all words before that word to get the index of the letter.

Amit Gold
  • 727
  • 7
  • 22
0

Probably not the fastest/best way but it will work. Used in rather than == in case there were quotations or other unexpected punctuation aswell! Hope this helps!!

def getWord(string, word):
    index = 0
    data = []
    for i in string.split(' '):
        if i.lower() in word.lower():
            data.append(index)
        index += 1
    return data
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19