Day 11

  • Computational Physics (PHYS 202)
  • Cal Poly, Spring 2015
  • Brian E. Granger

In class

  • Go over midterm

    • Discuss solutions
    • Grade distribution
  • Coding tips and help

    • Difference between return and print.
    • Transforming data between different container types.
    • How to count things.
    • Comments on performance.
  • Fetch today's material:

      nbgrader fetch phys202-2015 day11
      nbgrader fetch phys202-2015 assignment08

Coding tips and help

Different between return and print

A function that has no return statement will always return None:


In [ ]:
def f(x):
    print(x**2)

In [ ]:
a = f(2)

In [ ]:
print(a)

If you want a function to do anything useful, you have to return something:


In [4]:
def g(x):
    return x**2

In [5]:
b = g(2)

In [6]:
print(b)


4

Transforming container types

String can be turned into lists and tuples:


In [11]:
import random
alpha = 'abcdefghijklmnopqrstuvwxyz'

In [21]:
l = [random.choice(alpha) for i in range(10)]
l


Out[21]:
['l', 'r', 's', 'o', 'c', 'w', 'a', 'x', 'i', 'u']

In [22]:
s = ''.join(l)
s


Out[22]:
'lrsocwaxiu'

In [25]:
for c in s:
    print(c)


l
r
s
o
c
w
a
x
i
u

In [27]:
[c.upper() for c in s]


Out[27]:
['L', 'R', 'S', 'O', 'C', 'W', 'A', 'X', 'I', 'U']

In [23]:
list(s)


Out[23]:
['l', 'r', 's', 'o', 'c', 'w', 'a', 'x', 'i', 'u']

In [24]:
tuple(s)


Out[24]:
('l', 'r', 's', 'o', 'c', 'w', 'a', 'x', 'i', 'u')

In [29]:
digits = str(1001023039)

In [31]:
list(digits)


Out[31]:
['1', '0', '0', '1', '0', '2', '3', '0', '3', '9']

In [32]:
[int(d) for d in digits]


Out[32]:
[1, 0, 0, 1, 0, 2, 3, 0, 3, 9]

How to count things


In [38]:
def random_string(n):
    return ''.join([random.choice(alpha) for i in range(n)])

In [39]:
random_string(100)


Out[39]:
'ckggkxljgihymvcfwnpksgkpdoarugriwbtftuyhjxrotjmxambqrpsjsbbferfdcyvqzjyosxihsyrqdlprkidfljkanwtltgga'

In [46]:
def count0(seq):
    counts = {}
    for s in seq:
        counts[s] = seq.count(s)
    return counts

In [65]:
rs = random_string(10)
count0(rs), rs


Out[65]:
({'c': 1, 'd': 1, 'e': 1, 'k': 2, 'n': 1, 'q': 1, 's': 1, 'v': 1, 'w': 1},
 'cwnsqkkedv')

In [52]:
%timeit count0(random_string(10000))


10 loops, best of 3: 118 ms per loop

In [72]:
def count1(seq):
    counts = {}
    for s in seq:
        if s in counts:
            counts[s] += 1
        else:
            counts[s] = 1
    return counts

In [73]:
rs = random_string(10)
count1(rs), rs


Out[73]:
({'b': 1, 'c': 2, 'f': 1, 'l': 1, 'n': 1, 'q': 1, 'v': 1, 'y': 1, 'z': 1},
 'qlcfvcnbyz')

In [74]:
%timeit count1(random_string(10000))


100 loops, best of 3: 12.3 ms per loop

In [75]:
from collections import defaultdict

def count2(seq):
    counts = defaultdict(int)
    for s in seq:
        counts[s] += 1
    return counts

In [76]:
rs = random_string(10)
count2(rs), rs


Out[76]:
(defaultdict(<class 'int'>, {'t': 1, 'j': 1, 'r': 1, 'n': 1, 'v': 3, 'y': 1, 'e': 1, 'p': 1}),
 'pytvenjrvv')

In [77]:
%timeit count2(random_string(10000))


100 loops, best of 3: 11.9 ms per loop

In [78]:
from collections import Counter

In [84]:
def count3(seq):
    return dict(Counter(seq))

In [85]:
rs = random_string(10)
count3(rs), rs


Out[85]:
({'b': 1,
  'c': 1,
  'f': 1,
  'h': 1,
  'i': 1,
  'k': 1,
  'p': 1,
  's': 1,
  'v': 1,
  'y': 1},
 'fvhkypbisc')

In [86]:
%timeit count3(random_string(10000))


100 loops, best of 3: 11.6 ms per loop

Out of class

  • Complete assignment08 by Wednesday (Day 12).