Home > front end >  How to create a dictionary with the unique items of a list as key and the count of unique items as v
How to create a dictionary with the unique items of a list as key and the count of unique items as v

Time:05-25

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.]

CodePudding user response:

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})

CodePudding user response:

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}

CodePudding user response:

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}

CodePudding user response:

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}
  • Related