How to find and group similar permutations of 4 digits
<p>I am not good at these, but please bear with me. I have a set of numbers from my database / list, all are 4 digit numbers with the numbers between and include 0000 through 9999.</p>

<p>Say the list is:</p>

<pre><code>[1234, 4354, 6554, 2134, 3214, 5456, 9911, 1199]
</code></pre>

<p>Basically, I want to group them in such a way</p>

<pre><code>1234, 2134, 3214 is group A
6554, 5456 is group B
9911, 1199 is group C
4354 is group D
</code></pre>

<p>where the list items in each group all contain the same numbers - i.e, Group A are all made up of the numbers 1, 2, 3, and 4. Then I will find the len(group A), len(group B), len(group C), len(group D)...
and then sort them in decreasing manner.</p>

<p>How to do it?
And if list is huge, is the method still works ok?</p>

<p>Not sure what you want to do about naming the groups, but you can use <code>itertools.groupby</code> after converting the ints to strings, and sorting those characters</p>

<pre><code>from itertools import groupby

l = [1234, 4354, 6554, 2134, 3214, 5456, 9911, 1199]

# ints to (int, sorted str)
s = map(lambda x: (x, ''.join(sorted(str(x)))), l)

# sort the list for groupby
s.sort(key=lambda kv: kv[1])

# print out just the ints of the groups
for _, g in groupby(s, lambda kv: kv[1]):
    print map(lambda kv: kv[0], g)
</code></pre>

<p>Output </p>

<pre><code>[9911, 1199]
[1234, 2134, 3214]
[4354]
[6554, 5456]
</code></pre>

python permutation