python counter转dict,如何将Counter对象转换为dict?

Data frame:

pair = collections.defaultdict(collections.Counter)

e.g.

pair = {'doc1': {'word1':4, 'word2':3},

'doc2': {'word1':2, 'word3':4},

'doc3': {'word2':2, 'word4':1},

...}

I want to keep the data frame but alter the type of this part {'word1':4, 'word2':3} {'word1':2, 'word3':4}``... It is now a Counter and I need a dict.

I tried this to get the data from pair, but I do not know how to create a dict for each doc:

new_pair = collections.defaultdict(collections.Counter)

for doc, tab in testing.form.items():

for word, freq in tab.items():

new_pair[doc][word] = freq

I do not want to change the output. I just need that in each doc, the data type is dict, not Counter.

解决方案

A Counter is already a dict - or, a subclass of it. But, if you really need exactly a dict for some reason, then its a one-liner:

>>> c = Counter(word1=4, word2=3)

>>> c

Counter({'word1': 4, 'word2': 3})

>>> dict(c)

{'word1': 4, 'word2': 3}

Any Mapping (anything that behaves like a dictionary) can be passed into dict, and you will get a dict with the same contents. There is no need to iterate over it to construct it yourself.

This gives you one loop, with one line in the body instead of a nested loop. But any code of the form:

thing = a new empty collection

for elem in old_thing:

Add something to do with elem to thing

Can usually be done in one line using a generator expression or a list, set or dict comprehension. We're building a dict, so a dict comprehension (the Examples section is what you're most interested in) seems likely. I'll leave coming up with it as an exercise for the reader. ;-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值