Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.08 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why can't I search through the dictionary I created (Python)?
  2. from collections import defaultdict
  3. import operator
  4. def readFile(fileHandle):
  5.     d = defaultdict(int)
  6.     with open(fileHandle, "r") as myfile:
  7.         for currline in myfile:
  8.             for word in currline.split():
  9.                 d[word] +=1
  10.     return d
  11.  
  12. def reverseLookup(dictionary, value):
  13.     for key in dictionary.keys():
  14.         if dictionary[key] == value:
  15.             return key
  16.     return None
  17.  
  18. afile = raw_input ("What is the absolute file path: ")
  19. print readFile (afile)
  20.  
  21. choice = raw_input ("Would you like to (1) Query Word Count (2) Print top words to a new document     (3) Exit: ")
  22. if (choice == "1"):
  23.     query = raw_input ("What word would like to look up? ")
  24.     print reverseLookup(readFile(afile), query)
  25. if (choice == "2"):
  26.     f = open("new.txt", "a")
  27.     d = dict(int)
  28.     for w in text.split():
  29.         d[w] += 1
  30.     f.write(d)
  31.     file.close (f)
  32. if (choice == "3"):
  33.     print "The EXIT has HAPPENED"
  34. else:
  35.     print "Error"
  36.        
  37. from collections import defaultdict
  38.  
  39. def readFile(fileHandle):
  40.     d = defaultdict(int)  # Access to undefined keys creates a entry with value 0
  41.     with open(fileHandle, "r") as myfile:   # File will automatically be closed
  42.         for currline in myfile:             # Loop through file line-by-line
  43.             for word in currline.strip().split(): # Loop through words w/o CRLF
  44.                 d[word] +=1                 # Increase word counter
  45.     return d
  46.        
  47. def reverseLookup(dictionary, value):
  48.     for key in dictionary.keys():
  49.         if dictionary[key] == value:
  50.             return key
  51.     return None
  52.        
  53. import operator
  54. from collections import defaultdict
  55.  
  56. d = defaultdict(int)
  57. numbers_dict = {}
  58.  
  59. def readFile(fileHandle):
  60.     with open(fileHandle, "r") as myfile:
  61.         for currline in myfile:
  62.             for word in currline.split():
  63.                 d[word] +=1
  64.     return d
  65.  
  66.  
  67. def prepareReverse():
  68.     for (k,v) in d.items():
  69.         old_list = numbers_dict.get(v, [])
  70.         new_list = old_list << k
  71.         numbers_dict[v]=new_list
  72.  
  73. def reverseLookup(v):
  74.     numbers_dict[v]