Python Co-occurrence matrix of words and phrases
<p>I'm working with two text files. One contains a list of 58 words (L1), and the other one contains 1173 phrases (L2). I want to check <code>for i in range(len(L1))</code> and <code>for j in range(len(L1))</code> the co-occurrence in <code>L2</code>.</p>

<p>For example:</p>

<pre><code>L1 = ['b', 'c', 'd', 'e', 't', 'w', 'x', 'y', 'z']
L2 = ['the onion', 'be your self', 'great zoo', 'x men', 'corn day']

for i in range(len(L1)):
    for j in range(len(L1)):
        for s in range(len(L2)):
            if L1[i] in L2[s] and L1[j] in L2[s]:
                output = L1[i], L1[j], L2[s]
                print output
</code></pre>

<p>Output (example <code>'be your self'</code> from <code>L2</code>):</p>

<pre><code>('b', 'b', 'be your self')
('b', 'e', 'be your self')
('b', 'y', 'be your self')
('e', 'b', 'be your self')
('e', 'e', 'be your self')
('e', 'y', 'be your self')
('y', 'b', 'be your self')
('y', 'e', 'be your self')
('y', 'y', 'be your self')
</code></pre>

<p>The output shows what I want, but in order visualize data, I need also to return the times <code>L1[j]</code>concurs with <code>L1[i]</code>.</p>

<p>For example:</p>

<pre><code>  b e y
b 1 1 1
e 1 2 1
y 1 1 1
</code></pre>

<p>Should I use <code>pandas</code> or <code>numpy</code> in order to return this result?</p>

<p>I found this question about co-occurrence matrix but I didn't find and specific answer. <a href="https://stackoverflow.com/questions/15982746/efficient-algorithm-for-finding-co-occurrence-matrix-of-phrases">efficient algorithm for finding co occurrence matrix of phrases</a></p>

<p>Thanks!</p>