Reputation: 63
I'm trying to write a function, index(), that takes as input the name of a text file and a list of words. For every word in the list, the function will find the lines in the text file where the word occurs and print the corresponding line numbers (where the numbering starts at 1).
Example:
index('sawyer.txt',['hello','cloud','cherry','tree'])
hello 824,965
cloud 583, 16403
cherry 345, 561, 7891
tree 11, 45, 651
I already have function that counts the total amount of lines in a text file. But how would I print each line number for each word in the list?
def file_len():
with open('sawyer.txt') as f:
for i, l in enumerate(f):
pass
return i + 1
Any help would be greatly appreciated.
Upvotes: 1
Views: 2656
Reputation: 613
You can use following code to do just that
>>> dict={}
>>> with open('sawyer.txt') as f:
... for i, l in enumerate(f):
... for word in l.strip().split(" "):
... if word in dict.keys():dict[word].append(i)
... else:dict[word]=[i]
...
>>> dict
The complete function for your case will be as follows:
def word_locator(file, list_of_words):
dict={}
with open(file) as f:
for i, l in enumerate(f):
for word in l.split(" "):
if word in list_of_words:
if word in dict.keys():dict[word].append(i)
else:dict[word]=[i]
return dict
word_location_dictionary = word_locator('sawyer.txt', ['hello','cloud','cherry','tree'])
for k, v in word_location_dictionary.items():
lines = map(str, v)
print k + " " + ' '.join(lines)
Upvotes: 2