[Python-ideas] Extremely weird itertools.permutations

Ron Adam ron3200 at gmail.com
Thu Oct 17 18:10:22 CEST 2013



On 10/12/2013 02:09 AM, David Mertz wrote:
> On Sat, Oct 12, 2013 at 12:02 AM, Neil Girdhar
> <mistersheik at gmail.com
> <mailto:mistersheik at gmail.com>> wrote:
>
>     Why not just use the standard python way to generalize this: "key"
>     rather than the nonstandard "filter_by".
>
>
> Yes, 'key' is a much better name than what I suggested.
>
> I'm not quite sure how best to implement this still.  I guess MRAB's
> recursive approach should work, even though I like the simplicity of my
> style that takes full advantage of the existing itertools.permutations()
> (and uses 1/3 as many lines of--I think clearer--code).  His has the
> advantage, however, that it doesn't require operator.lt
> <http://operator.lt>() to work... however, without benchmarking, I have a
> pretty strong feeling that my suggestion will be faster since it avoids all
> that recursive call overhead.  Maybe I'm wrong about that though.


I'd like to see some nice examples of how it's used.  It seems to me, There 
is some mixing of combination/permutation concepts.


def filter_repeats(itr):
     seen = set()
     for i in itr:
         if i in seen:
             continue
         seen.add(i)
         yield i

def unique_combinations(itr, length=None):
     if length == None:
         length = len(itr)
     return it.product(filter_repeats(itr), repeat=length)


This one isn't the same as yours, but it's an example of filter_repeats. 
While that isn't the most efficient way in some cases, filtering repeat 
items out of things is a fairly common problem.

Cheers,
    Ron



More information about the Python-ideas mailing list