0

This is probably straightforward but I'm new to this sort of programming and struggling to wrap my head around what I need to do.

I'm trying to build a list of filters to apply to an image. I have seven filters I can apply and each filter can either be 0 or 1 (on or off). The filters are:

filters = [
    'Exposure',
    'Noise',
    'Pressure',
    'XQ Mix',
    'Invert',
    'Desaturate',
    'Equalise'
]

What I want to produce is every possible permutation of these filters in their states. There should be 128 possible permutations (2^7) but when I run the following code I get 5080 permutations:

perms = permutations(filters)
perm_count = 0
for p in perms:
    print(p)
    perm_count = perm_count + 1

print str(perm_count) + ' total permutations'

I'm likely using the wrong method – all this is doing is shuffling the sequence of filters, which I don't care about.

I tried updating the filter list to have two items for each filter, eg. ['Exposure0', 'Exposure1'] etc, but running combinations(filters, 7) against this gives me duplicate values (eg. both on and off states in the same list).

I'm struggling here – can anyone give me a nudge in the right direction for approaching something like this? Looking in the docs, something like product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy seems along the right lines but I still can't wrap my head around it. Help appreciated.

Matt Andrews
  • 2,868
  • 3
  • 31
  • 53

2 Answers2

3

What you are looking for as far as I understand is called a powerset. Here are some implementations for this method: Getting the subsets of a set in Python

Community
  • 1
  • 1
SurDin
  • 3,281
  • 4
  • 26
  • 28
  • Thank you – this was exactly what I wanted – effectively the missing items in the set are the "off" values and I can work out my filters. Much appreciated, thanks! – Matt Andrews Nov 17 '15 at 11:13
2

You are using permutations the wrong way. Look at the documentation:

>>> permutations('ABCD', 2)
AB AC AD BA BC BD CA CB CD DA DB DC

You are just mixing your filters values and creating all the possibilities, which is 7! = 5040.

What you want is the product of (0, 1) seven times.

>>> p = product((0, 1), repeat=7)
>>> print(len(p))
128
# [(0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 1), ...]
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • Thank you – have already accepted the other answer (because it gives me the actual list of "on" filters, whereas here I'd still need to associate the list of 0/1s with the filter sequence again. Thanks though, I've learned something about `itertools` today. – Matt Andrews Nov 17 '15 at 11:37
  • 1
    @MattAndrews Sure, the main thing is that you have resolved your issue, I hope you better understand how to use `product()` and `permutations()` now. – Delgan Nov 17 '15 at 11:40