Python中的sum()函数

huangapple go评论72阅读模式
英文:

sum() function in Python

问题

我对以下语法感到困惑:

def getClassiness(self):
        classiness_points = {
            "tophat": 2,
            "bowtie": 4,
            "monocle": 5
        }

        classiness = sum(classiness_points.get(item, 0) for item in self.items)
        return classiness

我理解以下的代码,但两者都是正确的。

def getClassiness(self):
        classiness_points = {
            "tophat": 2,
            "bowtie": 4,
            "monocle": 5
        }
        classiness = 0
        for item in self.items:
            classiness += classiness_points.get(item, 0)
        return classiness

我猜我不理解的是生成器表达式的工作原理。我理解sum()接受一个可迭代对象作为参数并返回元素的总和。我本以为应该是这样的:

classiness = sum(classiness_points.get(item, 0) for item in self.items)
        return classiness

更好的写法应该是这样的:

classiness = sum(classiness_points.get(item, 0) for item in self.items)
        return classiness

因为我习惯将表达式放在for循环之后。这可能只是语法问题。

英文:

I am confused with the following syntax:

def getClassiness(self):
        classiness_points = {
            "tophat": 2,
            "bowtie": 4,
            "monocle": 5
        }

        classiness = sum(classiness_points.get(item, 0) for item in self.items)
        return classiness

I understand the following one, but both are correct.

def getClassiness(self):
        classiness_points = {
            "tophat": 2,
            "bowtie": 4,
            "monocle": 5
        }
        classiness = 0
        for item in self.items:
            classiness += classiness_points.get(item, 0)
        return classiness

I guess what I do not understand is how a generator expression works. I understand that sum() takes an iterable as its argument and returns the sum of the elements. I would have thought of:

 classiness = sum(classiness_points.get(item, 0) for item in self.items)
        return classiness

better this way,

 classiness = sum( for item in self.items: classiness_points.get(item, 0))
        return classiness

Because I am used to place the expression after the for loop
It must be just syntax.

答案1

得分: 3

以下是翻译好的部分:

classiness_points = {'tophat': 2, 'bowtie': 4, 'monocle': 5}
items = ['tophat', 'monocle', 'shirt']

生成器的作用几乎与列表推导式相同:

[classiness_points.get(item, 0) for item in items]
# [2, 5, 0]

除了它只在被请求时生成项目,sum 通过迭代生成器的项目来执行这一操作:

sum(classiness_points.get(item, 0) for item in items)
# 7

因此,这与以下代码大致等效:

sum([classiness_points.get(item, 0) for item in items])

不同之处在于,在一种情况下,Python 预先生成一个要传递给 sum 的列表,而在另一种情况下,sum 本身通过迭代生成器的元素来执行操作。

英文:

Assuming this input:

classiness_points = {'tophat': 2, 'bowtie': 4, 'monocle': 5}
items = ['tophat', 'monocle', 'shirt']

What a generator does is almost the same as a list comprehension:

[classiness_points.get(item, 0) for item in items]
# [2, 5, 0]

Except it only produces the items when requested, which sum is doing by iterating over the generator's items.

sum(classiness_points.get(item, 0) for item in items)
# 7

So, this is more or less equivalent to:

sum([classiness_points.get(item, 0) for item in items])

Except in one case, python pre-generates a list to pass to sum. In the other sum is iterating by itself over the generators' elements.

huangapple
  • 本文由 发表于 2023年5月14日 13:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定