0

I know that if I have a dictionary filled with with x = {unique : count} then I can use prop_dict = dict((k, round(v/sum(x.values()),2)) for k,v in x.items()), but I do not know how to get there from an array/list.

Here is some sample data:

arr = [0., 137.,   3.,   1.,   1.,   5.,   2.,   2.,   8.,  31., 155.,
       3., 233.,  72., 302.,  66., 416.,   1., 148., 200., 237., 238.,
       354., 383., 422., 192.,  48.,  78., 136.,  15., 111.,   5.,  21.]
GT1992
  • 79
  • 6
  • https://docs.python.org/3/library/collections.html#collections.Counter – Nick May 25 '22 at 13:40
  • Does this answer your question? [python get count of unique values per key and unique count of value in keys](https://stackoverflow.com/questions/43981034/python-get-count-of-unique-values-per-key-and-unique-count-of-value-in-keys) – Nick May 25 '22 at 13:46
  • Almost, I needed to get to the dict first! – GT1992 May 25 '22 at 13:54
  • Does this answer your question? [How do I count occurrence of unique values inside a list](https://stackoverflow.com/questions/12282232/how-do-i-count-occurrence-of-unique-values-inside-a-list) – Ynjxsjmh May 25 '22 at 14:32

4 Answers4

3

Python has the collections.Counter class that will solve that for you:


In [1]: from collections import Counter                                                                                     

In [2]: arr = [0., 137.,   3.,   1.,   1.,   5.,   2.,   2.,   8.,  31., 155., 
   ...:        3., 233.,  72., 302.,  66., 416.,   1., 148., 200., 237., 238., 
   ...:        354., 383., 422., 192.,  48.,  78., 136.,  15., 111.,   5.,  21.]                                            

In [3]: Counter(arr)                                                                                                        
Out[3]: 
Counter({0.0: 1,
         137.0: 1,
         3.0: 2,
         1.0: 3,
         5.0: 2,
         2.0: 2,
         8.0: 1,
         31.0: 1,
         155.0: 1,
         233.0: 1,
         72.0: 1,
         302.0: 1,
         66.0: 1,
         416.0: 1,
         148.0: 1,
         200.0: 1,
         237.0: 1,
         238.0: 1,
         354.0: 1,
         383.0: 1,
         422.0: 1,
         192.0: 1,
         48.0: 1,
         78.0: 1,
         136.0: 1,
         15.0: 1,
         111.0: 1,
         21.0: 1})

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

Tell me if this works for you:

arr = [0., 137.,   3.,   1.,   1.,   5.,   2.,   2.,   8.,  31., 155.,
       3., 233.,  72., 302.,  66., 416.,   1., 148., 200., 237., 238.,
       354., 383., 422., 192.,  48.,  78., 136.,  15., 111.,   5.,  21.]


def list_to_dict_unique(arr):
    """Convert a list of element in a dictionary with key the elemnt and value is the number of occurence
    """
    d = {}
    for i in arr:
        d[i] = d.get(i, 0) + 1
    return d


if "__main__" == __name__:
    print(list_to_dict_unique(arr))

Result:

{0.0: 1, 137.0: 1, 3.0: 2, 1.0: 3, 5.0: 2, 2.0: 2, 8.0: 1, 31.0: 1, 155.0: 1, 233.0: 1, 72.0: 1, 302.0: 1, 66.0: 1, 416.0: 1, 148.0: 1, 200.0: 1, 237.0: 1, 238.0: 1, 354.0: 1, 383.0: 1, 422.0: 1, 192.0: 1, 48.0: 1, 78.0: 1, 136.0: 1, 15.0: 1, 111.0: 1, 21.0: 1}
Tau n Ro
  • 108
  • 8
  • About the pattern: `d[i] = d.get(i, 0) + 1` which is the correct way to deal with numbers, Python's dictionaries have it built-in the `setdefault` method - but it will only work when you are mutable values (like dicts and lists). I thought you'd like to know about it : https://docs.python.org/3/library/stdtypes.html#dict.setdefault – jsbueno May 25 '22 at 14:32
  • I didn't know it, it's really interesting! thank you so much ^^ – Tau n Ro May 25 '22 at 14:42
0

Another method:

l = [0., 137.,   3.,   1.,   1.,   5.,   2.,   2.,   8.,  31., 155.,
     3., 233.,  72., 302.,  66., 416.,   1., 148., 200., 237., 238.,
     354., 383., 422., 192.,  48.,  78., 136.,  15., 111.,   5.,  21.]

x = dict([x,l.count(x)] for x in set(l))

prop_dict = dict((k, round(v/sum(x.values()),2)) for k,v in x.items())
prop_dict

Result:

{0.0: 0.03,
 1.0: 0.09,
 2.0: 0.06,
 3.0: 0.06,
 5.0: 0.06,
 8.0: 0.03,
 15.0: 0.03,
 21.0: 0.03,
 31.0: 0.03,
 48.0: 0.03,
 66.0: 0.03,
 72.0: 0.03,
 78.0: 0.03,
 111.0: 0.03,
 136.0: 0.03,
 137.0: 0.03,
 148.0: 0.03,
 155.0: 0.03,
 192.0: 0.03,
 200.0: 0.03,
 233.0: 0.03,
 237.0: 0.03,
 238.0: 0.03,
 302.0: 0.03,
 354.0: 0.03,
 383.0: 0.03,
 416.0: 0.03,
 422.0: 0.03}
Drakax
  • 1,305
  • 3
  • 9
0

Since tag pandas let us do value_counts

d = pd.Series(arr).value_counts().to_dict()

Out[6]: 
{1.0: 3,
 3.0: 2,
 5.0: 2,
 2.0: 2,
 0.0: 1,
 237.0: 1,
 111.0: 1,
 15.0: 1,
 136.0: 1,
 78.0: 1,
 48.0: 1,
 192.0: 1,
 422.0: 1,
 383.0: 1,
 354.0: 1,
 238.0: 1,
 148.0: 1,
 200.0: 1,
 137.0: 1,
 416.0: 1,
 66.0: 1,
 302.0: 1,
 72.0: 1,
 233.0: 1,
 155.0: 1,
 31.0: 1,
 8.0: 1,
 21.0: 1}
BENY
  • 317,841
  • 20
  • 164
  • 234