python histogram one-liner

前端 未结 9 1045
一生所求
一生所求 2020-12-02 09:10

There are many ways to write a Python program that computes a histogram.

By histogram, I mean a function that counts the occurrence of objects in an iterable

9条回答
  •  Happy的楠姐
    2020-12-02 09:32

    For python 2.7, you can use this small list comprehension:

    v = list('abracadabra')
    print {x: v.count(x) for x in set(v)}
    

提交回复
热议问题