2
$\begingroup$

I have a list with expressions like

{1,3,5,a,c,b,3,d}

I want to generate all outputs with permutations of {a,b,c,d}, so for example a rule like

/. {a->a,b->d, c->c, d->b)

In total shere should be k! new lists. For the above example I also do not know how to make a replacement like b<->d which would save some time.

I found the Function PermutationReplace, and something like

PermutationReplace[{1,2,3},SymmetricGroup[3]]

would give me all 6 Permutations of 123. But I dont know how I can apply something like that were the elements I want to permute are arbitrary numbers/characters/variables.

I appreciate any help, and thanks for reading.

$\endgroup$
1
$\begingroup$
f[L_, s_] := With[{pos = Union @@ Lookup[PositionIndex[L], s, {}]},
               Permute[L, Cycles[{pos[[#]]}]] & /@ Permutations[Range[Length[pos]]]]

f[{1, 3, 5, a, c, b, 3, d}, {a, b, c, d}]
(* {{1, 3, 5, d, a, c, 3, b}, {1, 3, 5, b, a, d, 3, c},
    {1, 3, 5, d, b, a, 3, c}, {1, 3, 5, c, d, a, 3, b},
    {1, 3, 5, b, d, c, 3, a}, {1, 3, 5, c, b, d, 3, a},
    {1, 3, 5, c, d, a, 3, b}, {1, 3, 5, c, b, d, 3, a},
    {1, 3, 5, b, d, c, 3, a}, {1, 3, 5, d, a, c, 3, b},
    {1, 3, 5, d, b, a, 3, c}, {1, 3, 5, b, a, d, 3, c},
    {1, 3, 5, b, a, d, 3, c}, {1, 3, 5, b, d, c, 3, a},
    {1, 3, 5, c, b, d, 3, a}, {1, 3, 5, d, b, a, 3, c},
    {1, 3, 5, d, a, c, 3, b}, {1, 3, 5, c, d, a, 3, b},
    {1, 3, 5, d, a, c, 3, b}, {1, 3, 5, d, b, a, 3, c},
    {1, 3, 5, c, d, a, 3, b}, {1, 3, 5, b, d, c, 3, a},
    {1, 3, 5, b, a, d, 3, c}, {1, 3, 5, c, b, d, 3, a}} *)

If L has duplicates then the following can be used to avoid duplicates in the result

f[L_, s_] := Module[{res = L,
   pos = Union @@ Lookup[PositionIndex[L], s, {}]},
  (res[[pos]] = #; res) & /@ Permutations[L[[pos]]]]

f[{5, a, d, 3, d}, {a, b, c, d}]
(* {{5, a, d, 3, d}, {5, d, a, 3, d}, {5, d, d, 3, a}} *)
$\endgroup$
1
$\begingroup$

Here is a simple solution based on Inner product:

rules[vec_] := Inner[Rule, Permutations[vec], vec, List];

Examples:

rules[{a, b, c}]

{{a->a,b->b,c->c},{a->a,c->b,b->c},{b->a,a->b,c->c},{b->a,c->b,a->c},{c->a,a->b,b->c},{c->a,b->b,a->c}}

{1, 3, 5, a, c, b, 3, d} /. rules[{a, b, c, d}]

{{1,3,5,a,c,b,3,d},{1,3,5,a,d,b,3,c},{1,3,5,a,b,c,3,d},{1,3,5,a,b,d,3,c},{1,3,5,a,d,c,3,b},{1,3,5,a,c,d,3,b},{1,3,5,b,c,a,3,d},{1,3,5,b,d,a,3,c},{1,3,5,c,b,a,3,d},{1,3,5,d,b,a,3,c},{1,3,5,c,d,a,3,b},{1,3,5,d,c,a,3,b},{1,3,5,b,a,c,3,d},{1,3,5,b,a,d,3,c},{1,3,5,c,a,b,3,d},{1,3,5,d,a,b,3,c},{1,3,5,c,a,d,3,b},{1,3,5,d,a,c,3,b},{1,3,5,b,d,c,3,a},{1,3,5,b,c,d,3,a},{1,3,5,c,d,b,3,a},{1,3,5,d,c,b,3,a},{1,3,5,c,b,d,3,a},{1,3,5,d,b,c,3,a}}

$\endgroup$
1
$\begingroup$
ClearAll[g]
g[list_, sublist_]:= list /. AssociationThread[sublist, #]& /@ Permutations[sublist]

g[{1, 3, 5, a, c, b, 3, d}, {a, b, c, d}]

{{1, 3, 5, a, c, b, 3, d}, {1, 3, 5, a, d, b, 3, c}, {1, 3, 5, a, b, c, 3, d},
{1, 3, 5, a, d, c, 3, b}, {1, 3, 5, a, b, d, 3, c}, {1, 3, 5, a, c, d, 3, b},
{1, 3, 5, b, c, a, 3, d}, {1, 3, 5, b, d, a, 3, c}, {1, 3, 5, b, a, c, 3, d},
{1, 3, 5, b, d, c, 3, a}, {1, 3, 5, b, a, d, 3, c}, {1, 3, 5, b, c, d, 3, a},
{1, 3, 5, c, b, a, 3, d}, {1, 3, 5, c, d, a, 3, b}, {1, 3, 5, c, a, b, 3, d},
{1, 3, 5, c, d, b, 3, a}, {1, 3, 5, c, a, d, 3, b}, {1, 3, 5, c, b, d, 3, a},
{1, 3, 5, d, b, a, 3, c}, {1, 3, 5, d, c, a, 3, b}, {1, 3, 5, d, a, b, 3, c},
{1, 3, 5, d, c, b, 3, a}, {1, 3, 5, d, a, c, 3, b}, {1, 3, 5, d, b, c, 3, a}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.