Skip to content

Instantly share code, notes, and snippets.

@anishmenon
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anishmenon/64180ba820f066e521f2 to your computer and use it in GitHub Desktop.
Save anishmenon/64180ba820f066e521f2 to your computer and use it in GitHub Desktop.
#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