-2

I am trying to create a dictionary of user input. This dictionary takes any user input (ex. "I like pie") and will list each value with its respective index. However, my buildIndex function will not return a the dictionary created within the function. Can anyone give some insight as to why this might be happening?

d = {}
def buildIndex(m):
    m = m.lower() 
    words = m.split()
    i = 0
    while i < len(words):
        nextWord = words[i]
        if nextWord in d:
            ref = [d[nextWord]]
            ref.append(i)
            d[nextWord] = ref
        else:
            d[nextWord] = i
        i += 1
    return d
  • 1
    Does it not return anything, or return something that you don't expect? Are you getting any errors? – Undo Nov 16 '15 at 00:03
  • 3
    Why are you overwriting d with {}? Whatever `d` value you pass to your method will be immediately overwritten when you do `d = {}`. – idjaw Nov 16 '15 at 00:06
  • 5
    Also, it would help if you can give an example of what is a typical input you are providing and what is the expected output. I ran your code and it does return a dictionary. – idjaw Nov 16 '15 at 00:07
  • It seems possible that you are passing `d` *to* your function because you are thinking the situation analogous to the way data structures are handled in `C` (where it is somewhat common to pass a pointer to a function which is intended to point to the data structure after the function returns). You don't need that sort of indirection in Python. Unless the intent is to modify rather than create the dictionary -- drop `d` from the input parameters. – John Coleman Nov 16 '15 at 00:15
  • I don't get the close-votes. He is asking for insight and clearly new to Python. This question is neither a case of `give-me-deh-codez` nor obviously out of scope -- he is showing us what he has tried, etc. – zxq9 Nov 16 '15 at 00:22

1 Answers1

1

You seem to be approaching this as if it is C. That is understandable, but overcomplicating your Python. Let's consider just a few things.

Overwriting an input variable immediately:

def buildIndex(m, d):
    d = {}

In Python we don't need to declare variables or make room for them. So this is much more clearly expressed as:

def build_index(m):
    d = {}

(You notice I changed from functionName camelCase to underscore_naming -- that is pretty common. CamelCase is more common for class names in Python.)

Using an explicit iterator variable:

Python has a concept of iterables. This means any list-of-things type object can be assigned on the fly:

while i < len(words):
    nextWord = words[i]

becomes:

for word in words:
    if word in d:
        # etc...

or even:

for word in m.split():
    # blahblah

Overwriting things in the middle of your procedure:

Consider carefully what this section is doing. Step through it in your head.

if nextWord in d:
    ref = [d[nextWord]]
    ref.append(i)
    d[nextWord] = ref
else:
    d[nextWord] = i

What is the purpose of your dictionary? Build a word count? Keep a dictionary of locations-within-the-input-string? ...?!? As it is, this doesn't appear to do anything particularly useful. Even if it did, why it is OK to have a list value in some places and a non-list integer value in others? Pick a type and stick to it -- that will make things much easier.

You asked for insight, not for anyone to do your homework for you -- very cool. Consider the points above carefully, and read the docs on dictionaries (there are functions in there that will help you a lot -- particularly the dict() constructor...).

You're not too far off -- just need to get some Python idioms down.

zxq9
  • 13,020
  • 1
  • 43
  • 60