Best way to turn word list into frequency dict

后端 未结 8 560
无人及你
无人及你 2020-12-03 17:07

What\'s the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?<

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 17:35

    Kind of

    from collections import defaultdict
    fq= defaultdict( int )
    for w in words:
        fq[w] += 1
    

    That usually works nicely.

提交回复
热议问题