简体   繁体   English

了解Python

[英]Making sense of Python

I am reading the book Programming Collective Intelligence, What exactly the following piece of python code do? 我正在阅读《编程集体智慧》这本书,以下的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]]) 

I am trying to play with the examples in Java. 我正在尝试使用Java中的示例。

Prefs is a map of person to movie ratings, movie ratings is another map of names to ratings. Prefs是人与电影分级的映射,电影分级是名称与分级的另一映射。

First it constructs a list containing the results from: 首先,它构造一个包含以下结果的列表:

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")

Then it adds those up. 然后将它们加起来。

This might be a little more readable if the call to pow were replaced with an explicit use of '**' exponentiation operator: 如果战俘通话用的明确使用“**”求幂运算符的替代这可能有点更具可读性:

sum_of_squares=sum([(prefs[person1][item]-prefs[person2][item])**2
                   for item in prefs[person1] if item in prefs[person2]])

Lifting out some invariants also helps readability: 提出一些不变式也有助于提高可读性:

p1_prefs = prefs[person1]
p2_prefs = prefs[person2]

sum_of_squares=sum([(p1_prefs[item]-p2_prefs[item])**2
                      for item in p1_prefs if item in p2_prefs])

Finally, in recent versions of Python, there is no need for the list comprehension notation, sum will accept a generator expression, so the []'s can also be removed: 最后,在最新版本的Python中,不需要列表理解符号,sum将接受生成器表达式,因此[]也可以删除:

sum_of_squares=sum((p1_prefs[item]-p2_prefs[item])**2
                      for item in p1_prefs if item in p2_prefs)

Seems a bit more straightforward now. 现在似乎更加简单了。

Ironically, in pursuit of readability, we have also done some performance optimization (two endeavors that are usually mutually exclusive): 具有讽刺意味的是,为了提高可读性,我们还进行了一些性能优化(通常是相互排斥的两项努力):

  • lifted invariants out of the loop 将不变式从循环中取出
  • replaced the function call pow with inline evaluation of '**' operator 用“ **”运算符的内联评估替换了函数调用pow
  • removed unnecessary construction of a list 删除了不必要的列表构造

Is this a great language or what?! 这是一种很棒的语言还是什么?

01 sum_of_squares =
02 sum(
03  [
04      pow(
05         prefs[person1][item]-prefs[person2][item],
06         2
07      ) 
08    for
09       item
10    in
11       prefs[person1]
12    if
13       item in prefs[person2]
14  ]
15 )

Sum (line 2) a list, that consists of the values computed in lines 4-7 for each 'item' defined in the list specified on line 11 which the condition on line 13 holds true for. 对一个列表求和(第2行),该列表由在第4-7行中为第11行指定的列表中定义的每个“项目”计算的值组成,第13行的条件适用。

It computes the sum of the squares of the difference between prefs[person1][item] and prefs[person2][item] , for every item in the prefs dictionary for person1 that is also in the prefs dictionary for person2 . 它计算之间的差的平方之和prefs[person1][item]prefs[person2][item]为每一个, itemprefs对于字典person1那也是在prefs字典person2

In other words, say both person1 and person2 have a rating for the film Ratatouille , with person1 rating it 5 stars, and person2 rating it 2 stars. 换句话说,说person1person2都为电影Ratatouille评分, person1评为5星, person2评为2星。

prefs[person1]['Ratatouille'] = 5
prefs[person2]['Ratatouille'] = 2

The square of the difference between person1 's rating and person2 's rating is 3^2 = 9 . person1的等级与person2的等级之间的差的平方是3^2 = 9

It's probably computing some kind of Variance . 它可能正在计算某种方差

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM