Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, string
- def word_counts(f):
- result = {}
- for line in f:
- word_list = line.strip().lower().split()
- last_word = locals().get('last_word', None)
- for idx, val in enumerate(word_list):
- val = val.strip(string.punctuation)
- if last_word:
- w1 = last_word
- else:
- w1 = val
- if idx + 1 < len(word_list):
- if last_word:
- w2 = val
- last_word = None
- else:
- w2 = word_list[idx + 1].strip(string.punctuation)
- if result.has_key(w1):
- if result[w1].has_key(w2):
- result[w1][w2] += 1
- else:
- result[w1].update({w2: 1})
- else:
- result[w1] = {w2: 1}
- if idx + 1 == len(word_list):
- last_word = val
- print result
- with open('input.txt') as f:
- word_counts(f)
- M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- def transpose(mat):
- '''
- INPUT: 2 dimensional list of integers
- OUTPUT: 2 dimensional list of integers
- Return the transpose of the matrix. You may assume that the matrix is not
- empty. You can do this using a double for loop in a list comprehension.
- There is also a solution using zip.
- Example:
- >>>
- [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
- '''
- # return [list(x) for x in zip(*mat)]
- # column1 = []
- # column2 = []
- # column3 = []
- # for line in mat:
- # column1.append[mat[line[0]]]
- # column2.append[mat[line[1]]]
- # column3.append[mat[line[2]]]
- #
- # print transpose(M)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement