Reputation: 35
in my code im looking to take a string of text from the user and separate it into individual words, then get the indexes of those words. Im doing it however im running into two problems. first of all i need the indexes to show alongside of the words (for ex: "analysis: [5]"). In my code the indexes print before the words. The second issue is that if there are duplicate words in the string, my code returns the index of the first duplicate. Also if there are duplicates of the word im trying to show all the indexes in which the word appears(for ex: "the:[4,6,8]". PS I have the .sort method commented out because i'm going to alphabetize all the indexed words. Thanks.
import string
def main():
print('Please enter some text to index')
user_input=input()
buildIndex(user_input)
def buildIndex(text):
words=text.split()
index=[]
for x in range(len(words)):
lower=str.lower(words[x])
index.append(lower)
#index.sort()
index_position=index.index(lower)
print(index_position)
print('\n'.join(index))
main()
Upvotes: 0
Views: 804
Reputation: 6301
The indices are printing before the words because that's what the code does. The first call to print
is inside a loop, so it executes once for each pass through the loop. The second (printing the words) is after the loop, so that's when they print.
Instead of making a list of strings, you want to map strings to the position(s) you saw them at; in python, you want to use a dict
(also written {}
) for that.
Since you don't use anything from the string
module, this code doesn't need this line:
import string
The basic technique for creating a dict mapping a key to a list of values looks like this:
d.setdefault(key, []).append(value)
The setdefault
method of a dict returns either the current value stored at key
or stores the value passed in the second parameter (in this case, an empty list) at key
and returns that. Appending a value to a list modifies it in-place, so this code adds to the list stored in the dict at key
(which is either an existing list or the new one we just passed in.
Your main
function is fine, so I'll just show an updated buildIndex
def buildIndex(text):
words=text.split()
index={} # changed from a list to a dict
for i, word in enumerate(words): # more pythonic than range(len(words))
lower = word.lower() # better than str.lower(word)
index.setdefault(lower, []).append(i)
for k, v in sorted(index.items()):
print(k, v)
return index
I added return index
, just in case you want to use it in further code (if you do, you probably want to remove the loop that prints the index, but that's up to you).
Upvotes: 1