Python - Checking if all and only the letters in a list match those in a string?
<p>I'm creating an <strong>Anagram Solver</strong> in <strong>Python 2.7</strong>.</p>

<p>The solver takes a user inputted anagram, converts each letter to a list item and then checks the list items against lines of a '.txt' file, appending any words that match the anagram's letters to a <code>possible_words</code> list, ready for printing.</p>

<p>It works... <em>almost!</em></p>

<hr>

<pre><code># Anagram_Solver.py

anagram = list(raw_input("Enter an Anagram: ").lower())

possible_words = []

with file('wordsEn.txt', 'r') as f:

    for line in f:

        if all(x in line + '\n' for x in anagram) and len(line) == len(anagram) + 1:

            line = line.strip()
            possible_words.append(line)

print "\n".join(possible_words)
</code></pre>

<hr>

<p>For anagrams with no duplicate letters it works fine, but for words such as 'hello', the output contains words such as 'helio, whole, holes', etc, as the solver doesn't seem to count the letter 'L' as being 2 separate entries?</p>

<p>What am I doing wrong? I feel like there is a simple solution that I'm missing?</p>

<p>Thanks!</p>

<p>This is probably easiest to solve using a <code>collections.Counter</code></p>

<pre><code>&gt;&gt;&gt; from collections import Counter
&gt;&gt;&gt; Counter('Hello') == Counter('loleH')
True
&gt;&gt;&gt; Counter('Hello') == Counter('loleHl')
False
</code></pre>

<p>The <code>Counter</code> will check that the letters and the number of times that each letter is present are the same.</p>

python list python-2.7 pattern-matching anagram