title: Efficient way to mount a list of word occurences on Python
body: <p>I want to mount a data structure stating the number of occurences and mapping them at the right order.</p>
  <p>For example:</p>
  <blockquote>
  <p>word_1 =&gt; 10 occurences</p>
  <p>word_2 =&gt; 5 occurences</p>
  <p>word_3 =&gt; 12 occurences</p>
  <p>word_4 =&gt; 2 ocurrences</p>
  </blockquote>
  <p>and each word has one id to represent it:</p>
  <pre><code>kw2id = ['word_1':0, 'word_2':1, 'word_3':2, 'word_4': 3]
  </code></pre>
  <p>so an ordered list would be:</p>
  <pre><code>ordered_vocab = [2, 0, 1, 3]
  </code></pre>
  <p>For instance my code is this...:</p>
  <pre><code>#build a vocabulary with the number of ocorrences
  vocab = {}
  count = 0
  for line in open(DATASET_FILE):
      for word in line.split():
          if word in vocab:
              vocab[word] += 1
          else:
              vocab[word] = 1
      count += 1
      if not count % 100000:
          print(count, &quot;documents processed&quot;)
  </code></pre>
  <p>How can I do this <strong>efficiently</strong>?</p>