Recitation01 Notes
From 6.006 Wiki
(Difference between revisions)
(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...) |
m (→Profiling list operations) |
||
(2 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
==== Profiling list operations ==== | ==== Profiling list operations ==== | ||
<pre> | <pre> | ||
- | # | + | # Recitation 06- WF3 |
# Last update: September 08, 2008 | # Last update: September 08, 2008 | ||
# | # | ||
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> |
Latest revision as of 04:16, 11 September 2008
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()