Recitation01 Notes

From 6.006 Wiki

Jump to: navigation, search

Profiling list operations

# Recitation 06- WF3
# Last update: September 08, 2008
#
# Profile adding items to a list using "+" vs. extend()
items = [[i] for i in range(50000)]

def list_concat():
    L = []
    for item in items:
        L = L + item
        
def list_extend():
    L = []
    for item in items:
        L.extend(item)
        
import profile
profile.run(list_concat())
profile.run(list_extend())

Rewriting count_frequency in document distance problem

def count_frequency_mod(word_list):
    """
    Return a list giving pairs of form: (word,frequency)
    """
    D = {}
    for new_word in word_list:
        if D.has_key(new_word):
            D[new_word] = D[new_word]+1
        else:
            D[new_word] = 1
    return D.items()
Personal tools