title: How to sum up the word count for each person in a dialogue?
body: <p><br>I'm starting to learn Python and I'm trying to write a program that would import a text file, count the total number of words, count the number of words in a specific paragraph (said by each participant, described by 'P1', 'P2' etc.), exclude these words (i.e. 'P1' etc.) from my word count, and print paragraphs separately.</p>
  <p>Thanks to @James Hurford I got this code:</p>
  <pre><code>words = None
  with open('data.txt') as f:
     words = f.read().split()
  total_words = len(words)
  print 'Total words:', total_words
  
  in_para = False
  para_type = None
  paragraph = list()
  for word in words:
    if ('P1' in word or
        'P2' in word or
        'P3' in word ):
        if in_para == False:
           in_para = True
           para_type = word
        else:
           print 'Words in paragraph', para_type, ':', len(paragraph)
           print ' '.join(paragraph)
           del paragraph[:]
           para_type = word
    else:
      paragraph.append(word)
  else:
    if in_para == True:
      print 'Words in last paragraph', para_type, ':', len(paragraph)
      print ' '.join(paragraph)
    else:
      print 'No words'
  </code></pre>
  <p>My text file looks like this:</p>
  <blockquote>
  <p>P1: Bla bla bla.</p>
  <p>P2: Bla bla bla bla.</p>
  <p>P1: Bla bla.</p>
  <p>P3: Bla.</p>
  </blockquote>
  <p>The next part I need to do is summing up the words for each participant. I can only print them, but I don't know how to return/reuse them.</p>
  <p>I would need a new variable with word count for each participant that I could manipulate later on, in addition to summing up all the words said by each participant, e.g.</p>
  <pre><code>P1all = sum of words in paragraph
  </code></pre>
  <p>Is there a way to count &quot;you're&quot; or &quot;it's&quot; etc. as two words?</p>
  <p>Any ideas how to solve it?</p>