0

My program's purpose is to find positions of iterations of words in a sentence, and the malfunctioning subroutine goes as follows.

def analyse(splitString):
wordToSearch = input("What word are you searching for instances of? ").lower()
for word in splitString:
    positionLibrary = ""
    positionInSplitString = 0
    instances = 0
    if word == wordToSearch:
        position = splitString.index(word)
        positionLibrary += str(position)
        print (position, word)
        instances += 1
    positionInSplitString += 1
return (positionLibrary, instances, wordToSearch)

Let "splitString" be the list form of the sentence "THE ALTERATION OF MOTION IS EVER PROPORTIONAL TO THE MOTIVE FORCE IMPRESSED AND IS MADE IN THE RIGHT LINE ON WHICH THAT FORCE IS IMPRESSED". Now, say I search for "impressed" in splitString, it returns What word are you searching for instances of? impressed 11 impressed 11 impressed ['the', 'alteration', 'of', 'motion', 'is', 'ever', 'proportional', 'to', 'the', 'motive', 'force', 'impressed', 'and', 'is', 'made', 'in', 'the', 'right', 'line', 'on', 'which', 'that', 'force', 'is', 'impressed'] wordToSearch impressed instances 1 positionLibrary 11 which tells me that the program somehow knows that there are 2 instances of "impressed" yet does not count the number of these into the "instances" variable (which seems unreliable and doesn't work.) positionLibrary, which is meant to store (as a string) a record of the positions of instances found, doesn't work. I believe this is because the program only returns the position of the FIRST INSTANCE of "impressed" as shown in 11 impressed 11 impressed .

Now, how would I make the program actually return any positions after the first instance of a word and make the "instances" variable work? I've searched far and wide and have not found a solution.

ayelike47
  • 1
  • 1

2 Answers2

0

You don't need to use the index() method since you're already looping through splitString. You just need an index or counter that keeps track of which iteration you're at. For that, you can use enumerate.

What about this:

def analyse(splitString, wordToSearch):
    positionLibrary = [j for j, word in enumerate(splitString) if word == wordToSearch]
    instances = len(positionLibrary)
    return (positionLibrary, instances)

splitString = ['the', 'alteration', 'of', 'motion', 'is', 'ever', 'proportional', 'to', 'the', 'motive', 'force', 'impressed', 'and', 'is', 'made', 'in', 'the', 'right', 'line', 'on', 'which', 'that', 'force', 'is', 'impressed']
print analyse(splitString, 'impressed')
# ([11, 24], 2)

If you do want to use index(), it can take a second argument which is the position at which you should start the search. For example,

print splitString.index('impressed') # 11
print splitString.index('impressed', 0) # 11
print splitString.index('impressed', 12) # 24
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
0

If you like try something like this:-

def index_count_search(sentance, search):
     searchedList = map(lambda x:x[0], filter(lambda (index, value): search == value, enumerate(sentance.split())))
     return (",".join(searchedList), len(searchedList), search)


wordToSearch = input("What word are you searching for instances of? ").lower()
print analyse("THE ALTERATION OF MOTION IS EVER PROPORTIONAL TO THE MOTIVE FORCE IMPRESSED AND IS MADE IN THE RIGHT LINE ON WHICH THAT FORCE IS IMPRESSED".lower(), wordToSearch)
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30