| | |
Problems with dictionaries
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
Assume I have a file of the following format:
a,1
b,2
c,3
d,4
Here is my code:
Assume I want to call function something. It cannot print d1 and d2 unless I add them to the main block. As soon as I add d1 and d2 to the "main" block and call either of the two functions, both return two empty dictionaries. Why does this happen? How can I fix it? Please help!
a,1
b,2
c,3
d,4
Here is my code:
Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return (d1, d2) def something(): print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1 = junk(f)[0] d2 = junk(f)[1]
Assume I want to call function something. It cannot print d1 and d2 unless I add them to the main block. As soon as I add d1 and d2 to the "main" block and call either of the two functions, both return two empty dictionaries. Why does this happen? How can I fix it? Please help!
•
•
Join Date: Dec 2006
Posts: 1,074
Reputation:
Solved Threads: 299
0
#2 Nov 8th, 2009
First you are not returning a dictionary. I've added a print statement to show that. Also, take a look at "returns" and "arguments" here http://www.penzilla.net/tutorials/python/functions/ for the "something" function.
Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return (d1, d2) def something(): print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1 = junk(f)[0] print "type d1 =", type(d1) d2 = junk(f)[1]
Last edited by woooee; Nov 8th, 2009 at 1:19 pm.
Linux counter #99383
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#3 Nov 8th, 2009
•
•
•
•
First you are not returning a dictionary. I've added a print statement to show that. Also, take a look at "returns" and "arguments" here http://www.penzilla.net/tutorials/python/functions/ for the "something" function.
Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return (d1, d2) def something(): print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1 = junk(f)[0] print "type d1 =", type(d1) d2 = junk(f)[1]
•
•
Join Date: Dec 2006
Posts: 1,074
Reputation:
Solved Threads: 299
0
#4 Nov 8th, 2009
Try this and then go to the page from the earlier link which explains why you returned a tuple.
Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return d1, d2 if __name__ == "__main__": f = open("filename.txt") d1_ret, d2_ret = junk(f)[0] print type(d1_ret)
Linux counter #99383
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#5 Nov 8th, 2009
•
•
•
•
Try this and then go to the page from the earlier link which explains why you returned a tuple.Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return d1, d2 if __name__ == "__main__": f = open("filename.txt") d1_ret, d2_ret = junk(f)[0] print type(d1_ret)
0
#6 Nov 8th, 2009
•
•
•
•
Assume I have a file of the following format:
a,1
b,2
c,3
d,4
Here is my code:
Python Syntax (Toggle Plain Text)
def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return (d1, d2) def something(): print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1 = junk(f)[0] d2 = junk(f)[1]
Assume I want to call function something. It cannot print d1 and d2 unless I add them to the main block. As soon as I add d1 and d2 to the "main" block and call either of the two functions, both return two empty dictionaries. Why does this happen? How can I fix it? Please help!
python Syntax (Toggle Plain Text)
"""assume file "filename.txt" has this content a,1 b,2 c,3 d,4 """ def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(",") # <---- delimiter is comma letters = columns[0] # <---- index starts with 0 numbers = columns[1] # <---- index again d1[letters] = numbers d2[numbers] = letters return d1, d2 def something(d1, d2): # <---- give function proper arguments print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1, d2 = junk(f) # expand the tuple that is returned something(d1, d2) # give function its needed arguments """my output --> {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} """
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#7 Nov 8th, 2009
•
•
•
•
Really just a series of somewhat careless beginner mistakes you can fix this way ...
python Syntax (Toggle Plain Text)
"""assume file "filename.txt" has this content a,1 b,2 c,3 d,4 """ def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(",") # <---- delimiter is comma letters = columns[0] # <---- index starts with 0 numbers = columns[1] # <---- index again d1[letters] = numbers d2[numbers] = letters return d1, d2 def something(d1, d2): # <---- give function proper arguments print d1 print d2 if __name__ == "__main__": f = open("filename.txt") d1, d2 = junk(f) # expand the tuple that is returned something(d1, d2) # give function its needed arguments """my output --> {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} """
Last edited by pyprog; Nov 8th, 2009 at 3:28 pm.
0
#8 Nov 8th, 2009
•
•
•
•
But how come if you call the function junk again after the program finished executing and showed the output, it will return two empty dicts?
You can fix your code this way ...
python Syntax (Toggle Plain Text)
def junk(fh): d1 = {} d2 = {} for line in fh: columns = line.split(",") letters = columns[0] numbers = columns[1] d1[letters] = numbers d2[numbers] = letters return d1, d2 def something(d1, d2): print d1 print d2 if __name__ == "__main__": fh = open("filename.txt") d1, d2 = junk(fh) something(d1, d2) print( '-'*50 ) # second time around # file handle fh is at the end of the file now # reset it to the start of the file fh.seek(0) d1, d2 = junk(fh) something(d1, d2) """my output --> {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} -------------------------------------------------- {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} """
python Syntax (Toggle Plain Text)
"""assume file "filename.txt" has this content a,1 b,2 c,3 d,4 """ def file2dictionary(fname): """ read data from file and create a dictionary d1 and its reverse d2 """ fh = open("filename.txt") d1 = {} d2 = {} for line in fh: columns = line.split(",") letters = columns[0] numbers = columns[1] d1[letters] = numbers d2[numbers] = letters fh.close() return d1, d2 def print_dict(d1, d2): """print the two dictionaries d1 and d2""" print(d1) print(d2) if __name__ == "__main__": fname = "filename.txt" d1, d2 = file2dictionary(fname) print_dict(d1, d2) print( '-'*50 ) # test second time around d1, d2 = file2dictionary(fname) print_dict(d1, d2) """my output --> {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} -------------------------------------------------- {'a': '1\n', 'c': '3\n', 'b': '2\n', 'd': '4'} {'3\n': 'c', '1\n': 'a', '4': 'd', '2\n': 'b'} """
If you use the print() function instead of the print statement this code will work with Python2 and Python3.
Last edited by vegaseat; Nov 8th, 2009 at 4:02 pm.
May 'the Google' be with you!
![]() |
Similar Threads
- If i use an illegal copy of windows,will it cause problems? (Windows NT / 2000 / XP)
- dumb question about lists (Python)
- Internet explorer problems (Web Browsers)
- Connection Problems (Networking Hardware Configuration)
- Pop up ads (Web Browsers)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: Pythagoras?
- Next Thread: Validate Raw Input
Views: 241 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro beginner builtin cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui heads homework import input java lapse library line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random raw_input recursion recursive redirect script scrolledtext singleton socket sqlite ssh string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable web-scrape wikipedia windows word wxpython






