How to find permutations in a list then remove them?
<p>I'm trying to find a permutation of another number in this txt file and then add them to a list:</p>

<pre><code>167
168
148
143
289
194 
683
491
</code></pre>

<p>In this file, the permutations are 491 and 194.</p>

<p>This is my code:</p>

<pre><code>numberList = []

file = open("file.txt", "r")
for line in file:
    numberList.append(line)
</code></pre>

<p>Now they're added to <code>numberList</code>, how do I remove them (491 and 194) from this list.</p>

<p>There seem to be two problems: How to read numbers from a text file, and how to detect and filter out the duplicates w.r.t. permutations. For the first, you can use <code>strip</code> to get rid of the <code>\n</code> and convert to <code>int</code> to get actual numbers (although when using <code>int</code>, the first step is not really necessary). For filtering out the permutations, I suggest using a dictionary, associating each number with itself in sorted order. Since each permutation will be the same after sorting, this will group numbers that are permutations of each other. If you want to retain the original order, use <code>collections.OrderedDict</code>.</p>

<pre><code>import collections
numbers = collections.OrderedDict()

with open("file.txt") as f:
    for line in map(str.strip, f):
        print(line)
        key = ''.join(sorted(line))
        if key not in numbers:
            numbers[key] = []
        numbers[key].append(int(line))
    print(numbers)
</code></pre>

<p>Afterwards, <code>numbers</code> is </p>

<pre><code>OrderedDict([('167', [167]), ('168', [168]), ('148', [148]), ('134', [143]), ('289', [289]), ('149', [194, 491]), ('368', [683])])
</code></pre>

<p>To get the ordered sequence of numbers with no "permutation-duplicates", just get the first element from each of the value-lists:</p>

<pre><code>&gt;&gt;&gt; [v[0] for v in numbers.values()]
[167, 168, 148, 143, 289, 194, 683]
</code></pre>

<p>In case you want to remove <em>both</em> the duplicates, you can add an accordant condition:</p>

<pre><code>&gt;&gt;&gt; [v[0] for v in numbers.values() if len(v) == 1]
[167, 168, 148, 143, 289, 683]
</code></pre>

python permutation