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>Ok, let's do a bit of group theory:</p>

<p>assume you can decompose your numbers x into digits X[i] (removing the <code>\n</code> is really trivial, and surely someone else will cover this). </p>

<p>Then we know that, by how the decimal system works,</p>

<p><img src="http://i.stack.imgur.com/9ut2V.png" alt="$$x = \sum_{i=0}^{\log_{10} x} x[i] 10^i$$"></p>

<p>what we need to find is a function y=f(x) that maps x'es that are permutations of the same number to the same y, but x'es that aren't permutations to different y. </p>

<p>we can use the fact that the prime factorization of different numbers is different, and simply find the sum over exponents of a prime that isn't a digit (and larger than the length of the number*digits). This get's easier if we assume less than 9 digits, so we'll do that.</p>

<p>for this example, let's stick to 17 (which further limits our number of digits, but oh well).</p>

<p><img src="http://i.stack.imgur.com/Q1Yhi.png" alt="$$y= f(x) = \sum_{i=0}^{6} 17^{x[i]}$$"></p>

<p>so, now you'd use that function as <em>comparison key</em> in a python <code>set</code> and be done.</p>

<p>So, very naively (and very untested):</p>

<pre><code>class permutation(object):
    def __init__(self, x):
         """
         we'll assume x is an integer
         """
         digits = str(x)
         self.original = x
         self._hash = sum([17**int(digit) for digit in digits])
    def __hash__(self):
         return self._hash
    def __eq__(self,other):
         return self._hash == hash(other)
    def __ne__(self,other):
         return self._hash != hash(other)


permutation_free = set()

file = open("file.txt", "r")
for line in file:
    x = int(line)
    permutation_free.add(permutation(x))
print [perm.original for perm in permutation_free]
</code></pre>

<p><strong>EDIT:</strong> even tested it. </p>

python permutation