我可以根据两个值将列表理解缩减为两个列表吗?

发布于 2024-10-09 00:09:52 字数 472 浏览 2 评论 0原文

我有以下代码。

sum_review = reduce(add,[book['rw'] for book in books])
sum_rating = reduce(add,[book['rg'] for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

我想要的是这个。

sum_review,sum_rating = reduce(add,([book['rw'],[book['rg']) for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

显然这是行不通的。在没有常规循环的情况下如何解决这种冗余?

I've got the following code.

sum_review = reduce(add,[book['rw'] for book in books])
sum_rating = reduce(add,[book['rg'] for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

What I'd like is this.

sum_review,sum_rating = reduce(add,([book['rw'],[book['rg']) for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

Obviously this doesn't work. How can I solve this redundancy, without a regular loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送舟行 2024-10-16 00:09:52

我会避免在这里使用reduce。对于如此简单的事情,请使用 sum

sum_review = sum(book['rw'] for book in books)
sum_rating = sum(book['rg'] for book in books)

在我看来,这个更简单的版本不需要重构来消除冗余。只有两个项目(rwrg),我认为最好保持原样。

I'd avoid using reduce here. For something so simple use sum:

sum_review = sum(book['rw'] for book in books)
sum_rating = sum(book['rg'] for book in books)

In my opinion this simpler version doesn't need refactoring to remove redundancy. With just two items (rw and rg) I think it's best to just leave it as it is.

荒岛晴空 2024-10-16 00:09:52

有两种典型的简化代码的方法:

  1. 自上而下:首先获取值,然后使用 zip(*iterable) 转置它们。它也很酷,因为它只迭代集合一次:

    values = ((book["rw"], book["rg"]) for books in books)
    avg_review, avg_ rating = [sum(xs) / len(书籍) for xs in zip(*values)]
    
  2. 自下而上:创建一个函数来抽象操作:

    get_avg = lambda xs, attr: sum(x[attr] for x in xs) / len(xs)
    avg_review = get_avg(书籍, "rw")
    avg_ rating = get_avg(书籍,“rg”)
    

There are two typical approaches to simplify code:

  1. Top-down: get the values first and then transpose them with zip(*iterable). It's also cool because it only iterates the collection once:

    values = ((book["rw"], book["rg"]) for book in books)
    avg_review, avg_rating = [sum(xs) / len(books) for xs in zip(*values)]
    
  2. Bottom-up: create a function to abstract the operation:

    get_avg = lambda xs, attr: sum(x[attr] for x in xs) / len(xs)
    avg_review = get_avg(books, "rw")
    avg_rating = get_avg(books, "rg")
    
忱杏 2024-10-16 00:09:52
sum_review, sum_rating = reduce(lambda a,b: (a[0] + b[0], a[1]+b[1]), ((book['rw'], book['rg']) for book in books), (0,0) )
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

(已测试)

sum_review, sum_rating = reduce(lambda a,b: (a[0] + b[0], a[1]+b[1]), ((book['rw'], book['rg']) for book in books), (0,0) )
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

(tested)

闻呓 2024-10-16 00:09:52

你应该更喜欢清晰而不是优化。在使用 Python 的 3 年里,我只需要分析两次就可以发现性能瓶颈。您的原始代码清晰高效。将前两行压缩为一行会损害可读性并且几乎不会影响性能。

如果我必须修改你的代码,它会是这样的:(

avg_review = sum(book['rw'] for book in books) / len(books)
avg_rating = sum(book['rg'] for book in books) / len(books)

将五行代码减少到两行,并提高了清晰度。)

You should prefer clarity over optimization. In 3 years of using Python, I have only had to profile to discover performance bottlenecks twice. Your original code is clear and efficient. Compressing the first two lines into one hurts readability and barely impacts performance.

If I had to revise your code, it would like this:

avg_review = sum(book['rw'] for book in books) / len(books)
avg_rating = sum(book['rg'] for book in books) / len(books)

(That's five lines of code down to two with an improvement of clarity.)

不寐倦长更 2024-10-16 00:09:52

如何解决这种冗余

当然可以通过创建一个函数:

def average_value(items, key):
  values = [x[key] for x in items]
  return sum(items) / len(items)

avg_review, avg_rating = average_value(books, 'rw'), average_value(books, 'rg')

How can I solve this redundancy

By making a function, of course:

def average_value(items, key):
  values = [x[key] for x in items]
  return sum(items) / len(items)

avg_review, avg_rating = average_value(books, 'rw'), average_value(books, 'rg')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文