from __future__ import division
3/5
x = range(10)
x[1:5]
x = {}
x["dog"] = 5
x["asdfadfs"] = -3.44444
x
text = """
hello there class there class this is
"""
text.split()
counts = {}
for w in text.split():
if w not in counts:
counts[w] = 0
counts[w] += 1
counts
from collections import defaultdict
x = defaultdict(int)
x
x["asdf"]
{}[ "asdf" ]
x
x = defaultdict(int)
def f():
return 0
x = defaultdict(f)
x["asdf"]
x["qwer"]
x
counts
for w in counts:
print w
for w in counts.keys():
print w
counts.items()
s = 0
for word in counts:
s += counts[word]
s
s = 0
for (w,c) in counts.items():
print "word", w, " has count", c
s += c
s
sorted( [3,3,5,6,4,3,2,1] )
sorted(counts.items())
sorted(counts.items(), key=lambda (w,c): w)
sorted(counts.items(), key=lambda (w,c): c)
sorted(counts.items(), key=lambda (w,c): -c)
sorted(counts.items(), key=lambda (w,c): c, reverse=True)
counts
counts.values()
s = sum(counts.values())
s
[(w,c) for (w,c) in counts.items()]
[c for (w,c) in counts.items()]
[c**2 for (w,c) in counts.items()]
[(w,c/s) for (w,c) in counts.items()]
dict([(w,c/s) for (w,c) in counts.items()])
{w: c/s for (w,c) in counts.items() }