Skip to main content
edited body
Source Link
A. Kato
  • 8.6k
  • 1
  • 14
  • 38

I have a list expr of symbols.

Clear["Global`*"]; $Version (* 14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024) *) expr = {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f}; 

I want to move all X's to the right. This goal can be achieved by

wanted = Join[DeleteCases[expr, X], Cases[expr, X]] (* {a,d,c,b,f,c,a,a,b,b,b,f,c,b,d,a,f,d,d,c,f,f,c,f,X,X,X,X,X,X} *) 

But I want a rule-based solution using ReplaceRepeated[] because this is suitable for my ultimate goal. I tried

rule = {{p___, X, q_, r___} :> {p, q, X, r}}; expr2 = expr //. rule (* {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f,X,X} *) 

My question is: Why ReplaceRepeated[] stopped there? Am I doing something wrong?

I have a list expr of symbols.

Clear["Global`*"]; $Version (* 14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024) *) expr = {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f}; 

I want to move all X's to the right. This goal can be achieved by

wanted = Join[DeleteCases[expr, X], Cases[expr, X]] (* {a,d,c,b,f,c,a,a,b,b,b,f,c,b,d,a,f,d,d,c,f,f,c,f,X,X,X,X,X,X} *) 

But I want a rule-based solution using ReplaceRepeated[] because this is suitable for my ultimate goal. I tried

rule = {{p___, X, q_, r___} :> {p, q, X, r}}; expr2 = expr //. rule (* {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,f,f,c,f,X,X} *) 

My question is: Why ReplaceRepeated[] stopped there? Am I doing something wrong?

I have a list expr of symbols.

Clear["Global`*"]; $Version (* 14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024) *) expr = {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f}; 

I want to move all X's to the right. This goal can be achieved by

wanted = Join[DeleteCases[expr, X], Cases[expr, X]] (* {a,d,c,b,f,c,a,a,b,b,b,f,c,b,d,a,f,d,d,c,f,f,c,f,X,X,X,X,X,X} *) 

But I want a rule-based solution using ReplaceRepeated[] because this is suitable for my ultimate goal. I tried

rule = {{p___, X, q_, r___} :> {p, q, X, r}}; expr2 = expr //. rule (* {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f} *) 

My question is: Why ReplaceRepeated[] stopped there? Am I doing something wrong?

Source Link
A. Kato
  • 8.6k
  • 1
  • 14
  • 38

ReplaceRepeated ( //. ) does not work as expected

I have a list expr of symbols.

Clear["Global`*"]; $Version (* 14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024) *) expr = {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f}; 

I want to move all X's to the right. This goal can be achieved by

wanted = Join[DeleteCases[expr, X], Cases[expr, X]] (* {a,d,c,b,f,c,a,a,b,b,b,f,c,b,d,a,f,d,d,c,f,f,c,f,X,X,X,X,X,X} *) 

But I want a rule-based solution using ReplaceRepeated[] because this is suitable for my ultimate goal. I tried

rule = {{p___, X, q_, r___} :> {p, q, X, r}}; expr2 = expr //. rule (* {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,f,f,c,f,X,X} *) 

My question is: Why ReplaceRepeated[] stopped there? Am I doing something wrong?