In [3]:
from __future__ import division
3/5
Out[3]:
0.6
In [7]:
x = range(10)
In [11]:
x[1:5]
Out[11]:
[1, 2, 3, 4]
In [12]:
x = {}
In [17]:
x["dog"] = 5
x["asdfadfs"] = -3.44444
In [19]:
x
Out[19]:
{'asdfadfs': -3.44444, 'dog': 5}
In [20]:
text = """
hello there class there class this is
"""
In [23]:
text.split()
Out[23]:
['hello', 'there', 'class', 'there', 'class', 'this', 'is']
In [24]:
counts = {}
for w in text.split():
    if w not in counts:
        counts[w] = 0
    counts[w] += 1
counts
Out[24]:
{'class': 2, 'hello': 1, 'is': 1, 'there': 2, 'this': 1}
In [25]:
from collections import defaultdict
In [26]:
x = defaultdict(int)
In [28]:
x
Out[28]:
defaultdict(<type 'int'>, {})
In [29]:
x["asdf"]
Out[29]:
0
In [30]:
{}[ "asdf" ]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-30-ac6eadbb4dcd> in <module>()
----> 1 {}[ "asdf" ]

KeyError: 'asdf'
In [31]:
x
Out[31]:
defaultdict(<type 'int'>, {'asdf': 0})
In [34]:
x = defaultdict(int)
In [35]:
def f():
    return 0
x = defaultdict(f)
In [37]:
x["asdf"]
Out[37]:
0
In [38]:
x["qwer"]
Out[38]:
0
In [40]:
x
Out[40]:
defaultdict(<function f at 0x10274ff50>, {'qwer': 0, 'asdf': 0})
In [42]:
counts
Out[42]:
{'class': 2, 'hello': 1, 'is': 1, 'there': 2, 'this': 1}
In [44]:
for w in counts:
    print w
this
is
there
hello
class
In [45]:
for w in counts.keys():
    print w
    
this
is
there
hello
class
In [43]:
counts.items()
Out[43]:
[('this', 1), ('is', 1), ('there', 2), ('hello', 1), ('class', 2)]
In [46]:
s = 0
for word in counts:
    s += counts[word]
s
Out[46]:
7
In [49]:
s = 0
for (w,c) in counts.items():
    print "word", w, " has count", c
    s += c
s
word this  has count 1
word is  has count 1
word there  has count 2
word hello  has count 1
word class  has count 2
Out[49]:
7
In [52]:
sorted( [3,3,5,6,4,3,2,1] )
Out[52]:
[1, 2, 3, 3, 3, 4, 5, 6]
In [54]:
sorted(counts.items())
Out[54]:
[('class', 2), ('hello', 1), ('is', 1), ('there', 2), ('this', 1)]
In [55]:
sorted(counts.items(), key=lambda (w,c): w)
Out[55]:
[('class', 2), ('hello', 1), ('is', 1), ('there', 2), ('this', 1)]
In [57]:
sorted(counts.items(), key=lambda (w,c): c)
Out[57]:
[('this', 1), ('is', 1), ('hello', 1), ('there', 2), ('class', 2)]
In [58]:
sorted(counts.items(), key=lambda (w,c): -c)
Out[58]:
[('there', 2), ('class', 2), ('this', 1), ('is', 1), ('hello', 1)]
In [59]:
sorted(counts.items(), key=lambda (w,c): c, reverse=True)
Out[59]:
[('there', 2), ('class', 2), ('this', 1), ('is', 1), ('hello', 1)]
In [61]:
counts
Out[61]:
{'class': 2, 'hello': 1, 'is': 1, 'there': 2, 'this': 1}
In [63]:
counts.values()
Out[63]:
[1, 1, 2, 1, 2]
In [65]:
s = sum(counts.values())
In [67]:
s
Out[67]:
7
In [70]:
[(w,c) for (w,c) in counts.items()]
Out[70]:
[('this', 1), ('is', 1), ('there', 2), ('hello', 1), ('class', 2)]
In [71]:
[c for (w,c) in counts.items()]
Out[71]:
[1, 1, 2, 1, 2]
In [73]:
[c**2 for (w,c) in counts.items()]
Out[73]:
[1, 1, 4, 1, 4]
In [74]:
[(w,c/s) for (w,c) in counts.items()]
Out[74]:
[('this', 0.14285714285714285),
 ('is', 0.14285714285714285),
 ('there', 0.2857142857142857),
 ('hello', 0.14285714285714285),
 ('class', 0.2857142857142857)]
In [75]:
dict([(w,c/s) for (w,c) in counts.items()])
Out[75]:
{'class': 0.2857142857142857,
 'hello': 0.14285714285714285,
 'is': 0.14285714285714285,
 'there': 0.2857142857142857,
 'this': 0.14285714285714285}
In [78]:
{w: c/s for (w,c) in counts.items() }
Out[78]:
{'class': 0.2857142857142857,
 'hello': 0.14285714285714285,
 'is': 0.14285714285714285,
 'there': 0.2857142857142857,
 'this': 0.14285714285714285}
In [ ]: