How many items in a dictionary share the same value in Python

前端 未结 5 1642
小鲜肉
小鲜肉 2021-01-02 22:53

Is there a way to see how many items in a dictionary share the same value in Python?

Let\'s say that I have a dictionary like:

{\"a\": 600, \"b\": 75         


        
5条回答
  •  星月不相逢
    2021-01-02 23:37

    You could use itertools.groupby for this.

    import itertools
    x = {"a": 600, "b": 75, "c": 75, "d": 90}
    [(k, len(list(v))) for k, v in itertools.groupby(sorted(x.values()))]
    

提交回复
热议问题