I found Exercise 3.1 – Additional List Practice particularly interesting because it does not elaborate upon what is meant by “elements that are common to both lists”. Obviously, if, for example, 3
appears once in both lists, it should appear once in the resulting list. If 3
occurs once in one of the lists and twice in the other, it should, in my opinion, occur once in the result. But, how should we handle a situation in which an item occurs twice in both lists? It is up to us to decide how we would like to define the problem, since the instructions are not specific about this. In the result, I would represent each item the number of times which is the minimum of the number of times that it occurs in each of the two original lists. So, if 3
occurs five times in one list and seven times in the other, it would appear five times in the result.
In any case, your solution, @Cedar Mora, works well. I did, however, copy your code and replace this …
if not_repeated(intersection, number2):
… with …
if number2 not in intersection:
… thus eliminating the need for the not_repeated
function.
Here’s what I have for the solution, which reflects experimentation with how we define “elements that are common to both lists”. The code will run in Python 2 or 3.
# MIT 6.189 Homework 3 Exercise 3.1 Additional List Practice
# Three functions that implement list intersections
def list_intersection(list_1, list_2):
# return intersection of list_1 and list_2
return [item for item in list_1 if item in list_2]
print("list_intersection")
print(list_intersection([1, 3, 5], [5, 3, 1]))
print(list_intersection([1, 3, 6, 9], [10, 14, 3, 72, 9]))
print(list_intersection([2, 4, 6], [1, 3, 5]))
# but a problem occurs when duplicates appear in a list ...
print(list_intersection([1, 1, 2, 3, 5, 8, 13], [1, 3, 5, 7, 9, 11, 13]))
print(list_intersection([1, 3, 5, 7, 9, 11, 13], [1, 1, 2, 3, 5, 8, 13]))
def list_intersection_unique(list_1, list_2):
# return intersection of list_1 and list_2 - essentially, set intersection
# so, each item that appears in both lists appears once in the returned new list
return list({item for item in list_1 if item in list_2})
print("list_intersection_unique")
print(list_intersection_unique([1, 1, 2, 3, 5, 8, 13], [1, 3, 5, 7, 9, 11, 13]))
def list_intersection_minimalist(list_1, list_2):
# return a new list in which
# each item appears the lesser of the number of times
# that it appears either list
new_list = []
for item in set(list_1):
new_list += [item] * min(list_1.count(item), list_2.count(item))
return new_list
print("list_intersection_minimalist")
print(list_intersection_minimalist([1, 1, 1, 2, 3, 4], [1, 2, 1, 3, 7]))
print(list_intersection_minimalist([1, 2, 1, 3, 7], [1, 1, 1, 2, 3, 4]))
Output …
list_intersection
[1, 3, 5]
[3, 9]
[]
[1, 1, 3, 5, 13]
[1, 3, 5, 13]
list_intersection_unique
[1, 3, 5, 13]
list_intersection_minimalist
[1, 1, 2, 3]
[1, 1, 2, 3]
As indicated in the comments, a problem occurs with the first function, list_intersection
, when one of the lists includes a repeated item.