-9

If I have a dictionary from another function, how can I pass the dictionary within a new function? Eg.

From another function, I have tuples like this ('falseName', 'realName', positionOfMistake), eg. ('Milter', 'Miller', 4). I have a function that make a dictionary like this:

D={realName:{falseName:[positionOfMistake], falseName:[positionOfMistake]...},  
   realName:{falseName:[positionOfMistake]...}...} 

def addNameToDictionary(d, tup): 
    if not d.has_key(tup[0]): 
        d[tup[0]] = {} 
    d[tup[0]][tup[1]] = [tup[2]]

Now I need to write a function that takes a list of falseName's and return:

realName:
    falseName
    falseName
realName:...

My problem is how to call the dictionary from the function addNameToDictionary, to a new function?

I have tried the following:

def Names(nameList):
    D=addNameToDictionary(d, tup)
    print D

But I get:

NameError: global name 'd' is not defined

Anyone?

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
Linus Svendsson
  • 1,417
  • 6
  • 18
  • 21
  • 2
    Why don't you pass the dict to the new function, like you do in *addNameToDictionary*? – XORcist Dec 18 '11 at 12:16
  • what do you mean by "call the dictionary" and why don't you just pass the `dict` as an argument to the new function? – moooeeeep Dec 18 '11 at 12:23
  • 1
    well, *d* isn't anywhere in your code. All you have is (capital) *D*. What's more you don't return anything from *addNameToDictionary*. – XORcist Dec 18 '11 at 12:31
  • I have d as an argument in addNameToDictionary? – Linus Svendsson Dec 18 '11 at 12:33
  • You are trying to pass *d* to *addNameToDictionary* which is not defined. What's defined is capital *D*. – XORcist Dec 18 '11 at 12:37
  • 5
    Here is a list of tutorials. I recommend you pick one and work through it: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers. – XORcist Dec 18 '11 at 12:38
  • It's not like I havn't tried anywhere else. I am asking here because I cannot figure out how to fix it. Again, i CANNOT take other arguments to the new function than nameList. I understand d!=D, but it still isn't working! – Linus Svendsson Dec 18 '11 at 13:03
  • 2
    You've already asked http://stackoverflow.com/questions/8550912/python-dictionary-of-dictionaries/8550946#8550946 and got a lot of help. Perhaps you need to read some programming tutorials before asking more questions. –  Dec 18 '11 at 15:36
  • @LinusSvendsson: Why CAN YOU NOT add further arguments to your function? – Marcin Dec 19 '11 at 09:50

1 Answers1

0
def your_new_function(d, falseNames):
    # ... get data from d
    return {realname:(falseName1, falseName2)}
XORcist
  • 4,288
  • 24
  • 32