how to access a nested comprehensioned-list
Question: I have a working solution for creating a list some of random numbers, count
their occurrencies, and put the result in a dictionary which looks the
following:

    
    
    random_ints = [random.randint(0,4) for _ in range(6)]
    dic = {x:random_ints.count(x) for x in set(random_ints)])
    

so that for, say [0,2,1,2,1,4] I get {0: 1, 1: 2, 2: 2, 4:1}

I was wondering if its possible to express this in a one liner, preferably
without the use of a library function - I want to see what's possible with
python :) When I try to integrate the two lines in one I dont know howto
express the two references to the same comprehensioned list of random_ints
..??? I expected something like:

    
    
    dic = {x:random_ints.count(x) for x in set([random.randint(0,4) for _ in range(6)] as random_ints))
    

which of course does not work...

I looked (nested) list comprehensions up here on SO, but I could not apply the
solutions I found to my problem.

thanks, s.


Answer: Here is a one-liner that relies on `random` and `collections` modules.

    
    
    >>> import collections
    >>> import random
    >>> c = collections.Counter(random.randint(0, 6) for _ in range(100))
    >>> c
    Counter({2: 17, 1: 16, 0: 14, 3: 14, 4: 14, 5: 13, 6: 12})
    

