gpt4 book ai didi

Python:如何乘以 Counter 对象的值?

转载 作者:太空狗 更新时间:2023-10-29 23:59:05 24 4
gpt4 key购买 nike

我正在寻找一种方法来乘以 Counter 对象的值,即

a =  collections.Counter(one=1, two=2, three=3)
>>> Counter({'three': 3, 'two': 2, 'one': 1})

b = a*2
>>> Counter({'three': 6, 'two': 4, 'one': 2})

在 python 中执行此操作的标准方法是什么?

我为什么要这样做:我有一个稀疏特征向量(由 Counter 对象表示的词袋),我想对其进行归一化。

最佳答案

你可以这样做:

for k in a.keys():
a[k] = a[k] * 2

关于Python:如何乘以 Counter 对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23677982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com