Print the line number of a text file for each word from a list in python 3 without importing anything

DA_F Source

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.

pythonpython-3.x

Answers

answered 2 years ago wtpoo #1

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)

comments powered by Disqus