是否可以从 Counter() Python 获取二进制计数

Is it possible to get binary count from Counter() Python

使用Counter(),我想对列表中的变量进行二进制计数。因此,我不想获取每个变量的计数,而是想要一个 Counter() 变量,其中所有值都是一个。

所以对于给定的列表:

data = [1,2,3,4,5,62,3,4,5,1]

我希望输出为:

Counter({1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 62: 1})

而不是:

Counter({1: 2, 2: 1, 3: 2, 4: 2, 5: 2, 62: 1})

我知道我可以遍历计数器:

binary_count = {x: 1 for x in Counter(data)}

但是,这确实需要遍历字典一次,对我来说似乎没有必要。

这不是 Counter 的用途,因此无论您做什么都需要修改输出。

为什么不直接使用 Counter 开头呢?对于常规 dict.

这应该是微不足道的

哎呀,你甚至可以只使用 dict.fromkeys,所以:

>>> data = [1,2,3,4,5,62,3,4,5,1]
>>> dict.fromkeys(data, 1)
{1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 62: 1}