title: Counting the number of times a unique data double appears in double list python 3
body: <p>Say I have a double list in python <code>[[],[]]</code>:</p>
  
  <pre><code>doublelist = [["all", "the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste"], 
                ["the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste", "lumps"]]
  </code></pre>
  
  <p>I want to count how many times <code>doublelist[0][0] &amp; doublelist[1][0] = all, the</code> appear in the dual list. With the second [] being the index.</p>
  
  <p>For example you see one count of it at <code>doublelist[0][0] doublelist[1][0]</code> and another at <code>doublelist[0][6] doublelist[1][6]</code>.</p>
  
  <p>What code would I use in Python 3 to iterate through <code>doublelist[i][i]</code> grab each value set ex. <code>[["all"],["the"]]</code> and also an integer value for how many times that value set exists in the list.</p>
  
  <p>Ideally I'd like to output it to a triple list <code>triplelist[[i],[i],[i]]</code> that contains the <code>[i][i]</code> value and the integer in the third <code>[i]</code>.</p>
  
  <p>Example code:</p>
  
  <pre><code>for i in triplelist[0]:
      print(triplelist[0][i])
      print(triplelist[1][i])
      print(triplelist[2][i])
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&gt;"all"
  &gt;"the"
  &gt;2
  &gt;"the"
  &gt;"big"
  &gt;1
  &gt;"big"
  &gt;"dogs"
  &gt;1
  </code></pre>
  
  <p>etc...</p>
  
  <p>Also it would preferably skip duplicates so there wouldn't be 2 indexes in the list where <code>[i][i][i] = [[all],[the],[2]]</code> since there are 2 instances in the original list ([0][0] [1][0] &amp; [0][6] [1][6]). I just want all unique dual sets of words and the amount of times they appear in the original text.</p>
  
  <p>The purpose of the code is to see how often one word follows another word in a given text. It's for building essentially a smart Markov Chain Generator that weights word values. I already have the code to break the text into a dual list that contains the word in the first list and the following word in the second list for this purpose.</p>
  
  <p>Here is my current code for reference (the problem is after I initialize wordlisttriple, I don't know how to make it do what I described above after that):</p>
  
  <pre><code>#import
  import re #for regex expression below
  
  #main
  with open("text.txt") as rawdata:    #open text file and create a datastream
      rawtext = rawdata.read()    #read through the stream and create a string containing the text
  rawdata.close()    #close the datastream
  rawtext = rawtext.replace('\n', ' ')    #remove newline characters from text
  rawtext = rawtext.replace('\r', ' ')    #remove newline characters from text
  rawtext = rawtext.replace('--', ' -- ')    #break up blah--blah words so it can read 2 separate words blah -- blah
  pat = re.compile(r'([A-Z][^\.!?]*[\.!?])', re.M)    #regex pattern for grabbing everthing before a sentence ending punctuation
  sentencelist = []    #initialize list for sentences in text
  sentencelist = pat.findall(rawtext)    #apply regex pattern to string to create a list of all the sentences in the text
  firstwordlist = []    #initialize the list for the first word in each sentence
  for index, firstword in enumerate(sentencelist):    #enumerate through the sentence list
      sentenceindex = int(index)    #get the index for below operation
      firstword = sentencelist[sentenceindex].split(' ')[0]    #use split to only grab the first word in each sentence
      firstwordlist.append(firstword)    #append each sentence starting word to first word list
  rawtext = rawtext.replace(', ', ' , ')    #break up punctuation so they are not considered part of words
  rawtext = rawtext.replace('. ', ' . ')    #break up punctuation so they are not considered part of words
  rawtext = rawtext.replace('"', ' " ')    #break up punctuation so they are not considered part of words
  sentencelistforwords = []    #initialize sentence list for parsing words
  sentencelistforwords = pat.findall(rawtext)    #run the regex pattern again this time with the punctuation broken up by spaces
  wordsinsentencelist = []    #initialize list for all of the words that appear in each sentence
  for index, words in enumerate(sentencelist):    #enumerate through sentence list
      sentenceindex = int(index)    #grab the index for below operation
      words = sentencelist[sentenceindex].split(' ')    #split up the words in each sentence so we have a nested lists that contain each word in each sentence
      wordsinsentencelist.append(words)    #append above described to the list
  wordlist = []    #initialize list of all words
  wordlist = rawtext.split(' ')    #create list of all words by splitting the entire text by spaces
  wordlist = list(filter(None, wordlist))    #use filter to get rid of empty strings in the list
  wordlistdouble = [[], []]    #initialize the word list double to contain words and the words that follow them in sentences
  for index, word in enumerate(wordlist):    #enumerate through word list
      if(int(index) &lt; int(len(wordlist))-1):    #only go to 1 before the end of list so we don't get an index out of bounds error
          wordlistindex1 = int(index)    #grab index for first word
          wordlistindex2 = int(index)+1    #grab index for following word
          wordlistdouble[0].append(wordlist[wordlistindex1])    #append first word to first list of word list double
          wordlistdouble[1].append(wordlist[wordlistindex2])    #append following word to second list of word list double
  wordlisttriple = [[], [], []]    #initialize word list triple
  for index, unit in enumerate(wordlistdouble[0]):    #enumerate through word list double
      word1 = wordlistdouble[0][index]    #grab word at first list of word list double at the current index
      word2 = wordlistdouble[1][index]    #grab word at second list of word list double at the current index
      count = 0    #initialize word double data set counter
      wordlisttriple[0].append(word1)    #these need to be encapsulated in some kind of loop/if/for idk
      wordlisttriple[1].append(word2)    #these need to be encapsulated in some kind of loop/if/for idk
      wordlisttriple[2].append(count)    #these need to be encapsulated in some kind of loop/if/for idk
      #for index, unit1 in enumerate(wordlistdouble[0]):
          #if(wordlistdouble[0][int(index)] == word1 &amp;&amp; wordlistdouble[1][int(index)+1] == word2):
              #count++
  
  #sentencelist = list of all sentences
  #firstwordlist = list of words that start sentencelist
  #sentencelistforwords = list of all sentences mutated for ease of extracting words
  #wordsinsentencelist = list of lists containing all of the words in each sentence
  #wordlist = list of all words
  #wordlistdouble = dual list of all words plus the words that follow them
  </code></pre>
  
  <p>Any advice would be greatly appreciated. If I'm going about this the wrong way and there is an easier method to accomplish the same thing, that would also be amazing. Thank you!</p>