Last active
August 29, 2015 14:26
-
-
Save anishmenon/64180ba820f066e521f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Create a list of integers from 2 to 30, and another list from 3-50 | |
def 2_30(): | |
a = [] | |
return a.append(range(2,33)) | |
def 3_50(): | |
a = [] | |
return a.append(range(3,51)) | |
#define a function, which takes a filename and reads it from a local filesystem. Then it returns the 5 most common words in the file. | |
from collections import Counter | |
def (filepath): | |
with open(filepath) as f: | |
passage = f.read() | |
words = re.findall(r'\w+', passage) | |
words = [x for y in [l.split() for l in f.readlines()] for x in y] | |
print sorted([(w, words.count(w)) for w in set(words)], key = lambda x:x[1], reverse=True)[:10] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment