Вопрос

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 - sum and a list comprehension.

sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
                      for item in si])

First, the list comprehension.

[pow(prefs[person1][item]-prefs[person2][item],2) for item in si]

This can be broken down into a for loop that would look like this:

result_list = [] # Note that this is implicitly created
for item in si:
    result_list.append(pow(prefs[person1][item]-prefs[person2][item], 2))

That creates a list of values by running the pow function on each iteration, using each item in si and appending the result to result_list. Let's say that loop results in something like [1, 2, 3, 4] - now all that sum is doing is summing each element of the list and returning the result.

As to your question of what the sum function 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 with for loops, sum adds each item in the iterable (list in this case) and returns the total. There is also an optional start argument, but I would focus on understanding the base functionality first :)

Другие советы

This is just a list comprehension -so your 'for' loop is used to build a list of diff values in power of two

it is pretty the same like:

lVals = []
for item in si:
    lVals.append(pow(prefs[person1][item]-prefs[person2][item],2))

sum_of_squares = sum(lVals)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top