I understand the code below except for the sum function call below. I dont understand the logic of what exactly does a sum function accept as its argument? Whats the for loop in there? what is that thing??
def sim_distance(prefs,person1,person2):
# Get the list of shared_items
si={}
for item in prefs[person1]:
if item in prefs[person2]: si[item]=1
# if they have no ratings in common, return 0
if len(si)==0: return 0
# Add up the squares of all the differences
sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
for item in si])
return 1/(1+sum_of_squares)
So there are two concepts at work there –
sumand a list comprehension.First, the list comprehension.
This can be broken down into a
forloop that would look like this:That creates a list of values by running the
powfunction on each iteration, using eachiteminsiand appending the result toresult_list. Let’s say that loop results in something like[1, 2, 3, 4]– now all thatsumis doing is summing each element of the list and returning the result.As to your question of what the
sumfunction accepts as an argument, it is looking for an iterable, which is anything that can be iterated over (a string, a list, keys/values of dictionaries, etc.). Just like you see withforloops,sumadds each item in the iterable (list in this case) and returns the total. There is also an optionalstartargument, but I would focus on understanding the base functionality first 🙂