在词典列表中使用“减少” [英] Using 'reduce' on a list of dictionaries

查看:137
本文介绍了在词典列表中使用“减少”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的Python函数,它将所有具有 键的值相加。我正在为这个任务编写函数式编程。因此,我需要使用 list-comprehension map filter ,或减少。在这种情况下,我认为减少是一个合理的选择。

  def sum_favorites(msgs):
num_favorites = reduce(lambda x,y:x [likes] + y [likes],msgs)
return num_favorites


content1 = {likes:32,...}
content2 = {likes :8,...}
content3 = {likes:16,...}
contents = [content1,content2,content3]
print(sum_favorites(contents))

这个问题出现在我实际运行代码的时候。我似乎收到了以下内容: TypeError:'int'对象不是可自订的。对我来说,这个错误没有任何意义。如果 reduce 真正遍历给定的参数,那么传入lambda函数的每个项目都应该是一个字典 - 并且它们中的每一个都必须具有 对我来说,这个错误没有意义。如果reduce是真正迭代给定的参数,那么传入lambda函数的每个项目都应该是一个字典。

不,<传递给lambda的第一个参数(对于除第一个之外的所有调用)是前一次调用lambda的返回值。你的函数返回一个数字,所以它将被称为 x 是一个数字,而不是字典。



是解决这个问题的两种方法。可能更直接的是:

  num_favorites = reduce(lambda x,y:x + y ['likes'],msgs ,0)

0 是初始化器参数来减少,它为 x 提供第一个值。现在在每个调用中, x 是运行总和, y 是下一个字典。



另一种方式,只是为了表明它可以做到,是:

  result = reduce( lambda x,y:{'likes':x ['likes'] + y ['likes']},msgs)
num_favorites = result ['likes']

这使得lambda的返回值是一个带有 likes 键的字典,就像它的参数,所以我们在整个过程中使用相同的类型。在这种情况下,这是不必要的和浪费的,但是如果你聚合了多个键,这可能是一个有趣的方法。


I'm trying to write a simple Python function that sums all values that have the key as likes. I'm working with functional programming for this assignment. Thus, I am required to use either a list-comprehension, map, filter, or reduce. In this case, I see reduce as a reasonable option.

def sum_favorites(msgs):
     num_favorites = reduce(lambda x, y: x["likes"] + y["likes"], msgs)
     return num_favorites


content1 = {"likes": 32, ...}
content2 = {"likes": 8, ...}
content3 = {"likes": 16, ...}
contents = [content1, content2, content3]
print(sum_favorites(contents)) 

The issue comes to when I actually run the code. I seem to receive something along the lines of: TypeError: 'int' object is not subscriptable. To me, this error makes no sense. If reduce is truly iterating through the given parameter, then each item passed into the lambda-function should be a dictionary - and each of them definitely has a likes key in them. What is the issue, and what exactly does this Python error mean?

解决方案

To me, this error makes no sense. If reduce is truly iterating through the given parameter, then each item passed into the lambda-function should be a dictionary

No, the first parameter passed to the lambda (for all calls except the first) is the return value from the previous call to the lambda. Your function returns a number, so it will be called with x being a number, not a dictionary.

There are two ways to deal with this. The probably more straightforward one is:

num_favorites = reduce(lambda x, y: x + y['likes'], msgs, 0)

The 0 is the "initializer" argument to reduce, which provides the first value for x. Now in each call, x is the running sum and y is the next dictionary.

Another way, just to show that it can be done, is:

result = reduce(lambda x, y: { 'likes': x['likes'] + y['likes'] }, msgs)
num_favorites = result['likes']

which makes the return value of the lambda be a dict with a likes key, just like its arguments, so we're working with the same type the whole way through. In this case it's unnecessary and wasteful, but if you were aggregating more than one key, it might be an interesting approach.

这篇关于在词典列表中使用“减少”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆