Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import random, string
  2.  
  3. def word_counts(f):
  4. result = {}
  5. for line in f:
  6. word_list = line.strip().lower().split()
  7. last_word = locals().get('last_word', None)
  8.  
  9. for idx, val in enumerate(word_list):
  10. val = val.strip(string.punctuation)
  11. if last_word:
  12. w1 = last_word
  13. else:
  14. w1 = val
  15.  
  16. if idx + 1 < len(word_list):
  17. if last_word:
  18. w2 = val
  19. last_word = None
  20. else:
  21. w2 = word_list[idx + 1].strip(string.punctuation)
  22. if result.has_key(w1):
  23. if result[w1].has_key(w2):
  24. result[w1][w2] += 1
  25. else:
  26. result[w1].update({w2: 1})
  27. else:
  28. result[w1] = {w2: 1}
  29.  
  30. if idx + 1 == len(word_list):
  31. last_word = val
  32.  
  33. print result
  34.  
  35. with open('input.txt') as f:
  36. word_counts(f)
  37.  
  38. M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  39.  
  40. def transpose(mat):
  41. '''
  42. INPUT: 2 dimensional list of integers
  43. OUTPUT: 2 dimensional list of integers
  44.  
  45. Return the transpose of the matrix. You may assume that the matrix is not
  46. empty. You can do this using a double for loop in a list comprehension.
  47. There is also a solution using zip.
  48.  
  49. Example:
  50.  
  51. >>>
  52. [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
  53. '''
  54. # return [list(x) for x in zip(*mat)]
  55. # column1 = []
  56. # column2 = []
  57. # column3 = []
  58. # for line in mat:
  59. # column1.append[mat[line[0]]]
  60. # column2.append[mat[line[1]]]
  61. # column3.append[mat[line[2]]]
  62. #
  63. # print transpose(M)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement