Python中Counter数据结构的内存管理

In python, we have a very helpful data structure called Counter under the collections library. Basically we know that Counter store the data passed to it in an unordered dictionary manner.

counter = Counter("xxyz")

会给

Counter({'x': 2, 'y': 1, 'z': 1})

If I do, c['x'], I get 2.

When I do, c[0], I get 0.

If I do, c['1'], I get 0.

If I do, c['999999999999999999999999999999999999999999999999999999999999999990'], I get 0.

所以,我想知道,

  1. How Counter initialize its memory?
  2. How it allocates memory?

For example, I did not assign any value for the index 999999999999999999999999999999999999999999999999999999999999999990,

but when I'm trying to access c['999999999999999999999999999999999999999999999999999999999999999990'] I'm getting 0. Obviously it means, the Counter initializes everything with a default 0 value.

那么,内存分配是如何在后台发生的呢?

评论