Home > Software design >  Reverse Indexing a (small) dataset in Python without Panda or NumPy
Reverse Indexing a (small) dataset in Python without Panda or NumPy

Time:11-02

I'm a total python beginner trying to work my way through a python course and I'm stumped on this problem: I'm only returning

{'python': [2], 'rules': [2]} 
#  (all words should be lowercase)

Instead of the whole set, which should be:

{'python': [0, 2],'time': [0, 1],'it': [1],'is': [1],'that': [1],'rules':[2]}

any help would be appreciated!

from collections import defaultdict

dataset = [
    "Python time",
    "It is that TIME",
    "python rules"
 ] 

index_dictionary = {}

def reverse_index(dataset):
    
    for index in range(len(dataset)):
        phrase = dataset[index]
        words = phrase.lower()
        wordlist = words.split()

    for x in wordlist:
        if x in index_dictionary.keys():
            index_dictionary[x].append(index)
        else:
            index_dictionary[x] = [index]
    return (index_dictionary)

print(reverse_index(dataset))

CodePudding user response:

Your code almost works fine, you just have a small indentation mistake - you should have a nested for loop, as you want the wordlist to be updated for each sentence in the dataset:

def reverse_index(dataset):

    index_dictionary = {}

    for index in range(len(dataset)):
        phrase = dataset[index]
        words = phrase.lower()
        wordlist = words.split()

        for x in wordlist:
            if x in index_dictionary.keys():
                index_dictionary[x].append(index)
            else:
                index_dictionary[x] = [index]
    return (index_dictionary)
  • Related