从集合模块重新发明 Counter 类

发布于 2024-11-06 02:05:32 字数 318 浏览 0 评论 0原文

只是为了好玩,我尝试从 collections 模块重新发明 Counter 类。

我的意图很简单。给定一个列表,将元素映射到它出现的频率。

这是我写的:

>>> l
['a', 'a', 'b', 'c']
>>> d = {}
>>> for a in l:
    d[a] = l.count(a)
>>> d
{'a': 2, 'c': 1, 'b': 1}

只是想知道,这有多好或多坏?

Just for fun, I tried reinventing the Counter class from the collections module.

My intent was simple. Given a list, map the element to frequency in which it occurs.

Here is what I wrote:

>>> l
['a', 'a', 'b', 'c']
>>> d = {}
>>> for a in l:
    d[a] = l.count(a)
>>> d
{'a': 2, 'c': 1, 'b': 1}

Just wondering, how good or bad is this?

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

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

发布评论

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

评论(2

美人骨 2024-11-13 02:05:33

让我们专注于唯一相关的部分:

for a in l:
    d[a] = l.count(a)

这非常糟糕,它为每个成员调用 .count() ,并且 .count() 遍历每个成员本身,因此这种复杂性是O(n^2)。

这是 O(n):

l = ['a', 'a', 'b', 'c']
d = {}
for a in l:
    if a in d: # we saw a before
        d[a] += 1
    else:
        d[a] = 1

Let's concentrate on the only relevant part:

for a in l:
    d[a] = l.count(a)

This is pretty bad, it calls .count() for EVERY member and .count() goes over every member itself so this complexity is O(n^2).

This is O(n):

l = ['a', 'a', 'b', 'c']
d = {}
for a in l:
    if a in d: # we saw a before
        d[a] += 1
    else:
        d[a] = 1
琉璃繁缕 2024-11-13 02:05:33

好吧,它可能会更快,您正在迭代列表 n * n 次;其中 n 是列表的长度。相反,如果您只是遍历列表并在遇到该元素时在频率表中增加该元素的值,那么您会做更少的工作(n 多次,不包括我在字典中花费的任何时间)下注是恒定的。)

另一方面,您的版本在概念上简单明了。

Well it could be quicker, you are iterating over the list n * n times; where n is the length of the list. If you instead just went over the list and incremented that element's value in your frequency table whenever you encounter it, you would do less work (n many times so, not counting any time spent by the dictionary which I'd wager is constant.)

On the other hand, your version is conceptually simple and clear.

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