2

I wrote a simple function to calculate mode, but it seems one parameter does not pass successfully.

I initial countdict= dict() in the main function, then I pass it mod = mode(magList, countdict).

In mode(alist, countdict), countdict= dict(zip(alist,[0]*len(alist))). and countdict can print in mode.

but when I try to print(countdict) in main function, the output says it is empty. I check my code, it says in function mode, I unused countdict. How could that be possible.

The whole code is as following:

def mode(alist, countdict):
    countdict= dict(zip(alist,[0]*len(alist)))
    for x in alist:
        countdict[x]+=1
    maxcount =max(countdict.values()) 
    modelist = [ ]    
    for item in countdict:
        if countdict[item] == maxcount:
            modelist.append(item)
    return modelist



def makeMagnitudeList():
        quakefile = open("earthquakes.txt","r")
        headers = quakefile.readline()

        maglist = [ ]
        for aline in quakefile:
            vlist = aline.split()
            maglist.append(float(vlist[1]))
        return maglist

def mymain():
    magList = makeMagnitudeList()
    print(magList)
    countdict= dict()
    mod = mode(magList, countdict)
    print("mode: ", mod)
    print(countdict)

if __name__=='__main__':
    mymain()
user3754216
  • 107
  • 1
  • 1
  • 10
  • I suspect it's because `countdict` is getting reassigned in the first line of the `mode` function. – nick_w Oct 03 '14 at 07:39
  • 2
    You are missunderstanding some concepts about global and local variable. The `countdict` you have in your main is not the same as the `countdict` in the function, they are completely different variables. Maybe this help you [Python pass variable by reference](http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – AlvaroAV Oct 03 '14 at 07:42

2 Answers2

0

This line of code is your problem.

countdict= dict(zip(alist,[0]*len(alist)))

Python dictionaries are mutable objects and can be changed in a function, however, dictionary itself is not passed to the function. Only reference is passed and is passed by value. It means that when you assign a new dictionary to your countdict parameter, you lose the original reference pointing at countdict created in your mymain function.

pgiecek
  • 7,970
  • 4
  • 39
  • 47
0

As I said earlier, the line:

countdict= dict(zip(alist,[0]*len(alist)))

will wipe out the reference to countdict that you passed in. Because of this, the countdict variable you are printing is the original, empty dictionary. The question Liarez linked to: How do I pass a variable by reference? will help explain why this is happening.

To get around this, you could change the return statement in the mode function to:

return (modelist, countdict)

which will return a tuple containing both modelist and countdict. When calling this function, you would write:

(mod, countdict) = mode(magList, countdict)

ensuring that the modified countdict is returned, meaning that your print function call should not output an empty dictionary.

The other thing to note is that the countdict you are passing into mode is empty anyway, so you may find it better to simply not pass this argument in and have mode take only one parameter.

Community
  • 1
  • 1
nick_w
  • 14,758
  • 3
  • 51
  • 71