Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. __author__ = 'rok'
  2. from collections import defaultdict
  3. import operator
  4. filename = "u.data"
  5.  
  6. lines = [line.strip() for line in open(filename)]
  7. ocene_filmov = defaultdict(list)
  8. stevilo_ogledanih_filmov = defaultdict(int)
  9. for line in lines:
  10.     tokens = line.split("\t")
  11.     ocene_filmov[tokens[1]].append(int(tokens[2]))
  12.     stevilo_ogledanih_filmov[tokens[0]] += 1
  13.  
  14. stevilo_filmov_nad_20 = 0
  15.  
  16. out = "povprecne_ocene.out"
  17. data_out = "data.tab"
  18. povprecne_ocene = {}
  19.  
  20. for key, value in ocene_filmov.iteritems():
  21.     # print "Film", key, "ima", value, "ocene"
  22.     if len(value) >= 20:
  23.         stevilo_filmov_nad_20 += 1
  24.         avg = sum(value) / float(len(value))
  25.         povprecne_ocene[key] = avg
  26.  
  27. stolpci = ["user_id", "item_id", "rating", "timestamp"]
  28. tipi = ["d", "d", "c", "d"]
  29.  
  30. with open(data_out, "w") as f_out:
  31.     for stolpec in stolpci:
  32.         f_out.write(stolpec + "\t")
  33.     f_out.write("\n")
  34.     for tip in tipi:
  35.         f_out.write(tip + "\t")
  36.     f_out.write("\n")
  37.     # special nesto
  38.     f_out.write("\n")
  39.  
  40.     for line in lines:
  41.         tokens = line.split("\t")
  42.         if tokens[0] in povprecne_ocene.keys():
  43.             f_out.write(line + "\n")
  44.  
  45.  
  46.  
  47. sortirane_povp_ocene = sorted(povprecne_ocene.items(), key=operator.itemgetter(1))
  48.  
  49. print "10 najslabsih: "
  50.  
  51. i = 0
  52. while i != 10:
  53.     print sortirane_povp_ocene[i][0]
  54.     i += 1
  55.  
  56. print "10 najboljsih: "
  57.  
  58. i = 0
  59. while i != 10:
  60.     print sortirane_povp_ocene[len(sortirane_povp_ocene) - 1 - i][0]
  61.     i += 1
  62.  
  63. print "Gledanost filmov: "
  64. sortirani_filmi_po_gledanosti = sorted(stevilo_ogledanih_filmov.items(), key=operator.itemgetter(1))
  65.  
  66. print "10 najmanj gledanih: "
  67.  
  68. i = 0
  69. while i != 10:
  70.     print sortirani_filmi_po_gledanosti[i][0]
  71.     i += 1
  72.  
  73. print "10 najbolj gledanih: "
  74.  
  75. i = 0
  76. while i != 10:
  77.     print sortirani_filmi_po_gledanosti[len(sortirani_filmi_po_gledanosti) - 1 - i][0]
  78.     i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement