title: Frequency analysis of values produced by generator
body: <p>I'm doing some analysis of images, and I have a generator that gives me all pixels in image:</p>
  
  <pre><code>def pixels_g(img):
      w, h = img.shape
      for y in range(0, h):
          for x in range(0, w):
              yield img[y][x]
  </code></pre>
  
  <p>It's output if converted to list would be something like </p>
  
  <pre><code>[0, 1, 2, 5, 240, 5, ... ]
  </code></pre>
  
  <p>Now I'd like to get "frequency table" for it:</p>
  
  <pre><code>{
      0: 0,
      1: 5,
      2: 10,
      3: 0,
      4: 0,
      #snip
      255: 7
  }
  </code></pre>
  
  <p>I've found some useful options in <a href="https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python">this question</a>, but they all work with list, and I don't think that creating a list from my generator is a good idea - it can have <strong>millions of elements</strong>.</p>
  
  <p>I'm therefore looking for a way to do this while preserving the benefits of generator. I'll process many images and don't want to hog resources too much.</p>