3

I have 5 vertical strings which were generated in order of probability by my classifier. If the classifiers confidence was very high I want to keep that classification but for the low ones I would like to vary between all possible orders of combinations creating a list of all possible vertical strings.

so my data looks like this:

aa aa aa aa aa
ab ac aa ad ae
aa ab af ae ag

and I would like to get all of the possible ordered combinations

aa aa aa aa aa aa aa aa aa aa ...
ab ac aa ad ae ae ab ac aa ad ...
aa ab af ae ag aa ab af ae ag...

I tried itertools but I can't seem to find the right tool to do this. Does anyone know how to do this?

This is what I have tried so far:

import sys
import os
import itertools
from itertools import permutations

in_file = sys.argv[1]

f1 = open(in_file, 'r')

new_lines = []

for line in f1.readlines():
    line = line.strip()
    do stuff to replace my higher confidence matches...
        new_lines.append(line)

for x in new_lines:
    for a,b,c,d,e,f,g,h,i,j in permutations(x.split(), 10):
        print '{} {} {} {} {} {} {} {} {} {}'.format(a.rstrip('\n'), b.rstrip('\n'), c.rstrip('\n'), d.rstrip('\n'), e.rstrip('\n'), f.rstrip('\n'), g.rstrip('\n'), h.rstrip('\n'), i.rstrip('\n'), j.rstrip('\n'))

I tested this with 10 to make make sure it didn't explode but this does not seem to work the way I thought it would. If I put 5 it just gives me the same list I had before. Is there a way I can do this?

badner
  • 768
  • 2
  • 10
  • 31
  • So to confirm you want every possible unique sequence of n (10) objects from a set of x (5) inputs? – leumas95 Jul 29 '17 at 22:45
  • 1
    You can use `itertools.product` as described [here](https://stackoverflow.com/questions/23058028/how-to-get-all-mappings-between-two-lists?rq=1) – Gleland Jul 29 '17 at 22:49
  • @leumas95. I just put 10 to stop it. I really want all of the possible orders of each 5 element list. – badner Jul 29 '17 at 23:01
  • @Gleland, I don't think so because in each line I just have 5 elements and not two lists... but it is kind of on its way. – badner Jul 29 '17 at 23:03
  • @badner I see what you are doing, the limit is for the length of each permutation not the number of individual permutations. – leumas95 Jul 29 '17 at 23:10

1 Answers1

7

The Python itertools.permutations method takes in an iterable and an integer limit (r).

The integer is used to limit the length of each permutation, for example, if you had a list of permutations([1, 2, 3], 2) would give you [(1, 2),(1, 3),(2, 1),(2, 3),(3, 1),(3, 2)].

The limit must not be larger than the length of the input list or it will return an empty iterable.

See https://docs.python.org/2/library/itertools.html#itertools.permutations specifically "The number of items returned is n! / (n-r)! when 0 <= r <= n or zero when r > n."

leumas95
  • 377
  • 8
  • 22