-1

I'm writing a function where the user inputs a word, then inputs a string, and the function identifies all occurrences and the position of that word in a string (although it's actually converted to a list halfway through).

The code I have currently is only identifying the first occurrence of the word, and none further. It also doesn't identify the word if the word is the first word in the string, returning an empty list. It will also say the word's actual position - 1, as the first word is counted as zero.

I have attempted to curb this problem in two ways, the first of which doing a aString.insert(0, ' '), the second of which doing for i in __: if i == int: i += 1. Neither of these work.

Also, when doing the .insert, I tried putting a character in the space instead of, well, a space (as this part doesn't get printed anyway), but that didn't work.

Here is the code:

def wordlocator(word):
    yourWord = word
    print("You have chosen the following word: " +yourWord)
    aString = input("What string would you like to search for the given word?")
    aString = aString.lower()
    aString = aString.split()
    b = [(i, j) for i, j in enumerate(aString)]
    c = [(i, x) for i, x in b if x == yourWord]
    return c

The output I'm looking for is that if someone did...

wordlocator("word")
"please input a string generic message" "that is a word"
"4, word"

Currently that would work, but it'd print "3, word". If the string was "that is a word and this is also a word" then it'd ignore the further occurrence of "word". edit: Got it working now, used a simpler piece of code. Thanks for your help all!

  • 1
    *"the first word is counted as zero"* - yes, Python's indexing is 0-based. You can choose to `enumerate` starting from another value, though - see its documentation. – jonrsharpe Jun 28 '16 at 15:32
  • 1
    Python (and I think all languages I've used) index off zero. If you need the real number position just add 1. As for only getting the first usage -- you can either fix the list comprehension per one of the answers below or look into "yield" -- check this question [here](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python) – Lost Jun 28 '16 at 15:38
  • @Lost If I remember correctly, Cobol uses 1-indexing for arrays. Op, I can't reproduce your problem of finding only one occurrence, you code works fine for me. But maybe the problem is if the user type punctuation : `'word' != 'word.' != 'word,' ` – polku Jun 28 '16 at 15:50

1 Answers1

1

Try this:

def wordlocator(word):
    yourWord = word
    print("You have chosen the following word: " +yourWord)
    aString = raw_input("What string would you like to search for the given word?")
    aString = aString.lower()
    aString = aString.split()
    b = [(i+1, j) for i, j in enumerate(aString) if j == yourWord.lower()]

    return b

print wordlocator('word')

note that the list comprehension can be filtered on just the match you are looking for. Actually I just changed it

I get this:

What string would you like to search for the given word?This word is not that word is it?
[(2, 'word'), (6, 'word')]

Note that the index is off by one if that matters, add one to x in the comprehension

A new test: You have chosen the following word: word What string would you like to search for the given word?word is the word [(1, 'word'), (4, 'word')]

joel goldstick
  • 4,393
  • 6
  • 30
  • 46