Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all possible orders of a list [duplicate]

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?

like image 517
badner Avatar asked Oct 26 '25 16:10

badner


1 Answers

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."

like image 61
leumas95 Avatar answered Oct 28 '25 06:10

leumas95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!