从集合模块重新发明 Counter 类
只是为了好玩,我尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们专注于唯一相关的部分:
这非常糟糕,它为每个成员调用
.count()
,并且.count()
遍历每个成员本身,因此这种复杂性是O(n^2)。这是 O(n):
Let's concentrate on the only relevant part:
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):
好吧,它可能会更快,您正在迭代列表 n * n 次;其中
n
是列表的长度。相反,如果您只是遍历列表并在遇到该元素时在频率表中增加该元素的值,那么您会做更少的工作(n
多次,不包括我在字典中花费的任何时间)下注是恒定的。)另一方面,您的版本在概念上简单明了。
Well it could be quicker, you are iterating over the list
n * n
times; wheren
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.