





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























[x for x in lst1 if x > 2 for y in lst2 for z in lst if x + y + z < 8] res = [] # translation for x in lst1: if x > 2: for y in lst2: for z in lst3: if x + y + z > 8: res.append(x)
>>> k = [1,2,3] >>> i = iter(k) # could also use k.iter() >>> i.next() 1 >>> i.next() 2 >>> i.next() 3 >>> i.next() StopIteration
>>> import math >>> math.sqrt(9)
>>> from math import * >>> sqrt(9) # unclear where function defined #hw1.py def concatenate(seqs): return [seq for seq in seqs] # This is wrong
with hw1.py >>> import hw >>> assert hw1.concatenate([[1, 2], [3, 4 ]]) == [1, 2, 3, 4 ] #AssertionError >>> reload(hw1) #after fixing hw
>>> def myfun(b, c=3, d=āhelloā): return b + c >>> myfun(5,3,ābobā) 8 >>> myfun(5,3) 8 >>> myfun(5) 8
def print_everything(*args):
for count, thing in enumerate(args): print '{0}. {1}'.format(count, thing) >>> lst = ['a', āb', 'cā] >>> print_everything(*lst)
a
b
c >>> print_everything('a', āb', 'c')
def print_keyword_args(**kwargs):
for key, value in kwargs.iteritems(): #.items() is list print "%s = %s" % (key, value) >>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'} >>> print_keyword_args(**kwargs) first_name = John last_name = Doe >>> print_keyword_args(first_name="Johnā, last_name="Doe")
foo(arg1, *args, **kwargs) # one mandatory argument
def fib(n, fibs={}): if n in fibs: return fibs[n] if n <= 1: fibs[n] = n else: fibs[n] = fib(n-1) + fib(n-2) return fibs[n]
def compose(f, g, x): return f(g(x)) >>> compose(str, sum, [1,2,3]) '6'
from operator import itemgetter def calc_ngram(inputstring, nlen): ngram_list = [inputstring[x:x+nlen] for x in xrange(len(inputstring)-nlen+1)] ngram_freq = {} # dict for storing results for n in ngram_list: # collect the distinct n-grams and count if n in ngram_freq: ngram_freq[n] += 1 else: ngram_freq[n] = 1 # human counting numbers start at 1
(ascending/descending) return sorted(ngram_freq.iteritems(), key=itemgetter(1), reverse=True) http://times.jayliew.com/2010/05/20/a simple n gram calculator pyngram/