理解Python

Ham*_*aya 0 python

我正在阅读编程集体智能这本书,下面的python代码到底是做什么的?

  # Add up the squares of all the differences 
  sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2) 
                      for item in prefs[person1] if item in prefs[person2]]) 
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用Java中的示例.

Prefs是人物到电影收视率的地图,电影收视率是收视率的另一个名称地图.

and*_*oke 6

首先,它构造一个包含以下结果的列表:

for each item in prefs for person1:
    if that is also an item in the prefs for person2:
        find the difference between the number of prefs for that item for the two people
        and square it (Math.pow(x,2) is "x squared")
Run Code Online (Sandbox Code Playgroud)

然后它添加了那些.