all permutations of +-r, +-s

Viewed 817

Given two numbers r and s, I would like to get a list of all permutations of n +-r and m +-s. For example (with r=3.14 and s=2.71),

n = 1
m = 1
out = [
    (+r, +s), (+r, -s), (-r, +s), (-r, -s), 
    (+s, +r), (+s, -r), (-s, +r), (-s, -r)
    ]
n = 1
m = 2
out = [
    (+r, +s, +s), (+r, -s, +s), (-r, +s, +s), (-r, -s, +s), ...
    (+s, +r, +s), (-s, +r, +s), (+s, -r, +s), (-s, -r, +s), ...
    ...
    ]

With itertools.product([+r, -r], repeat=n) I can get the list of the rs and ss separately, and I'd only need to intertwine them, but I'm not sure if this is the right thing to do.

Efficiency is not overly important, so I wouldn't mind a solution that produces many repeated results only to make them unique afterwards.

3 Answers
Related