Recitation01 Notes

From 6.006 Wiki

(Difference between revisions)
Jump to: navigation, search
(New page: ==== Profiling list operations ==== <pre> # Author: Matthew Ng # Last update: September 08, 2008 # # Profile adding items to a list using "+" vs. extend() items = [[i] for i in range(50000...)
(Rewriting count_frequency in document distance problem)
Line 24: Line 24:
==== Rewriting count_frequency in document distance problem ====
==== Rewriting count_frequency in document distance problem ====
<pre>
<pre>
-
#
 
-
#
 
-
#
 
def count_frequency_mod(word_list):
def count_frequency_mod(word_list):
     """
     """
Line 38: Line 35:
             D[new_word] = 1
             D[new_word] = 1
     return D.items()
     return D.items()
 +
</pre>

Revision as of 04:14, 11 September 2008

Profiling list operations

# Author: Matthew Ng
# 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