cnt = Counter ("Hello")
cnt = Counter ([1,2,3,3,3])
I think the time complexities for the code above are O(N), where N is the length of each collection.
I didn't find the exact time complexity of Counter() object here . I really appreciate it if someone can confirm my guess.
As the source code and documentation show, Counter
is just a subclass of dict
, optimized for counting objects. Constructing it from an iterable has the time complexity of O(n)
, since it iterates all items and updates counters for each item in O(1)
constant time.
The time complexity of other dict
operations will also hold for Counter
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.