2
$\begingroup$

Suppose I have a list like {a, b, a, c}. How can I apply a "symbolic permutation" of the symbols a, b, and c such as a->b, b->c, c->a to produce {b, c, b, a}?

Replace[] with multiple rules applies them all sequentially, and I get {a, b, a, c} back. Permute[] can only change the positions of things in a list, not their names. PermutationReplace[] requires a permutation to be specified with integers, not with symbols.

$\endgroup$
4
  • 4
    $\begingroup$ You can try RepalceAll as in {a, b, a, c} /. {a -> b, b -> c, c -> a}. $\endgroup$ Jan 10, 2016 at 21:42
  • 1
    $\begingroup$ Replace[{a, b, a, c}, {a -> b, b -> c, c -> a}, 1] or Replace[{a, b, a, c}, {a -> b, b -> c, c -> a}, {1}] would also work. You need to specify the level specification because you want to replace level 1 (inside a list), not level 0; Replace replaces level 0 only by default. $\endgroup$ Jan 10, 2016 at 21:43
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jan 10, 2016 at 22:07
  • $\begingroup$ To clarify: My confusion was over how to use Replace[]. The problem wasn't that it applied all the rules in sequence; the problem was that I was applying the rules at the wrong level, and they weren't taking effect. When they are applied at the right level, only one takes effect on each element. $\endgroup$
    – G. Smith
    Jan 12, 2016 at 4:10

2 Answers 2

1
$\begingroup$

I realized that Map and Association can do the job:

Map[Association[a -> b, b -> c, c -> a][#] &, {a, b, a, c}]

I appreciate the quick answers from eldo and Kuba, but they were both too specific to my example. In general, I won't know the indexes of the elements I need to change, or the particular permutation I need to make.

For any arbitrary permutation, I can create an Association, and then Map each element of an arbitrary list.

$\endgroup$
2
  • $\begingroup$ I now see that b.gatessucks and JHM provided general answers in comments to my original question. Thanks! $\endgroup$
    – G. Smith
    Jan 11, 2016 at 1:58
  • 1
    $\begingroup$ Is there a way for me to approve an answer in a comment to the original question? $\endgroup$
    – G. Smith
    Jan 11, 2016 at 1:59
0
$\begingroup$
list = {a, b, a, c};

ReplacePart[list /. a -> b, {{2} -> c, {4} -> a}]

{b, c, b, a}

or simply

list[[{2, 4, 2, 1}]]

{b, c, b, a}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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