在python中求列表子集的总和

发布于 2024-11-09 15:22:44 字数 324 浏览 0 评论 0原文

这可能非常简单,我忽略了一些东西......

我有一长串整数,在本例中代表网站的每日访问者。我想要一份新的每周访客列表。因此,我需要从原始列表中获取七组,对它们求和,然后将它们添加到新列表中。

我的解决方案看起来相当暴力,不优雅:

numweeks = len(daily) / 7
weekly = []
for x in range(numweeks):
    y = x*7
    weekly.append(sum(visitors[y:y+7]))

是否有更有效或更Pythonic的方法来做到这一点?

This is probably very simple and I'm overlooking something...

I have a long list of integers, in this case representing daily visitors to a website. I want a new list of weekly visitors. So I need to get groups of seven from the original list, sum them, and add them to a new list.

My solution seems pretty brute force, inelegant:

numweeks = len(daily) / 7
weekly = []
for x in range(numweeks):
    y = x*7
    weekly.append(sum(visitors[y:y+7]))

Is there a more efficient, or more pythonic way of doing this?

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

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

发布评论

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

评论(3

情话已封尘 2024-11-16 15:22:44
weekly = [ sum(visitors[x:x+7]) for x in range(0, len(daily), 7)]

或者稍微不那么密集:

weekly = []
for x in range(0, len(daily), 7):
     weekly.append( sum(visitors[x:x+7]) )

或者,使用 numpy 模块。

by_week = numpy.reshape(visitors, (7, -1))
weekly = numpy.sum( by_week, axis = 1)

请注意,这要求访问者中的元素数量是 7 的倍数。还要求您安装 numpy。然而,它可能也比其他方法更有效。

或者对于 itertools 代码奖励:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

weekly = map(sum, grouper(7, visitors, 0))
weekly = [ sum(visitors[x:x+7]) for x in range(0, len(daily), 7)]

Or slightly less densely:

weekly = []
for x in range(0, len(daily), 7):
     weekly.append( sum(visitors[x:x+7]) )

Alternatively, using the numpy module.

by_week = numpy.reshape(visitors, (7, -1))
weekly = numpy.sum( by_week, axis = 1)

Note that this requires the number of elements in visitor be a multiple of 7. It also requires that you install numpy. However, its probably also more efficient then the other approaches.

Or for itertools code bonus:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

weekly = map(sum, grouper(7, visitors, 0))
她说她爱他 2024-11-16 15:22:44
>>> daily = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
>>> print [sum(daily[x:x+7]) for x in range(0, len(daily), 7)]
[28, 77, 105]

我不确定这是否是“Pythonic”,但我真的很喜欢Python的这一行东西。

血淋淋的细节:推导式

>>> daily = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
>>> print [sum(daily[x:x+7]) for x in range(0, len(daily), 7)]
[28, 77, 105]

I am not sure if this is "pythonic", but I truly love this one-line stuff of python.

Gory Details: Comprehensions

不美如何 2024-11-16 15:22:44

使用itertools.islice:

weekly = [sum(list(itertools.islice(daily, i, i+7)))
          for i in range(0, len(daily), 7)]

编辑:

或者使用math.fsum:

weekly = [math.fsum(itertools.islice(daily, i, i+7))
          for i in range(0, len(daily), 7)]

Using itertools.islice:

weekly = [sum(list(itertools.islice(daily, i, i+7)))
          for i in range(0, len(daily), 7)]

Edit:

or, with math.fsum:

weekly = [math.fsum(itertools.islice(daily, i, i+7))
          for i in range(0, len(daily), 7)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文