5
$\begingroup$

Consider I have the following list with no duplicates. I would like to replace {a,c} with a number.

lst = {a,b,c,d}

Result I want is {1, b, d}. This can be in any order.


I have tried the following using SequenceReplace[] which works only if the sequence I want to replace is in the same order as in lst.

SequenceReplace[{a, b, c, d}, {b, c} -> 1]

{a,1,d}

SequenceReplace[{a, b, c, d}, {a, c} -> 1]

{a,b,c,d}

Is there a way to get {1, b, d}?

$\endgroup$
6
  • $\begingroup$ Do you have more than one occurrence of a, ... , c, ... in your input list? If so then please update the question with more test cases. $\endgroup$
    – Syed
    Mar 25 at 16:18
  • 5
    $\begingroup$ You cold replace a with 1 and c with Nothing: lst /. {a -> 1, c -> Nothing} $\endgroup$ Mar 25 at 16:26
  • $\begingroup$ @Syed the list doesn't have any duplicates. I have updated the question $\endgroup$ Mar 25 at 16:31
  • 1
    $\begingroup$ If you do require to work with non-adjacent sequence items, here is a possible solution: SequenceReplace[lst, {g___, a, x___, c, y___} :> Sequence[g, 1, x, y]] $\endgroup$
    – Syed
    Mar 25 at 16:35
  • 1
    $\begingroup$ or just SequenceReplace[lst, {a, x___, c} -> Sequence[1, x]] $\endgroup$
    – Roman
    Mar 25 at 16:41

2 Answers 2

8
$\begingroup$

You may use OrderlessPatternSequence.

With

lst = Permutations[{a, b, c, d}]
{{a,b,c,d},{a,b,d,c},{a,c,b,d},{a,c,d,b},{a,d,b,c},{a,d,c,b}
,{b,a,c,d},{b,a,d,c},{b,c,a,d},{b,c,d,a},{b,d,a,c},{b,d,c,a}
,{c,a,b,d},{c,a,d,b},{c,b,a,d},{c,b,d,a},{c,d,a,b},{c,d,b,a}
,{d,a,b,c},{d,a,c,b},{d,b,a,c},{d,b,c,a},{d,c,a,b},{d,c,b,a}}

Then

lst /. {OrderlessPatternSequence[a, c, e___]} :> {1, e}
{{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d}
,{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d}
,{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d}
,{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d},{1,b,d}}

Hope this helps.

$\endgroup$
2
  • 2
    $\begingroup$ Since the pattern is orderless you don't need separate s,m,e. Just lst /. {OrderlessPatternSequence[a, c, x___]} :> {1, x} $\endgroup$ Mar 25 at 19:25
  • $\begingroup$ @SimonWoods Yes, you are correct. $\endgroup$
    – Edmund
    Mar 25 at 20:02
4
$\begingroup$
$Version

(* "13.0.1 for Mac OS X x86 (64-bit) (January 28, 2022)" *)

Clear["Global`*"]

rules[lst_, target_, value_] := 
 Flatten[{#[[1]] :> value, Thread[Rest[#] :> Nothing]}] &@
  Select[lst, MemberQ[target, #] &]

lst = {a, b, c, d};

target = {a, c};

lst /. rules[lst, target, 1]

(* {1, b, d} )

For a more complicated example

SeedRandom[1234];

lst = RandomSample[{a, b, c, d, e, f}]

(* {a, f, d, b, e, c} *)

target = {c, a, f};

lst /. rules[lst, target, 1]

(* {1, d, b, e} *)
$\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.