Skip to main content
added 34 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68

{1, 4, 27}1427

{1, 4, 27}

1427

added another approach labelled 12
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
  1. Laborious stuff, but works
  • $\#_1$

The following:

(Transpose[{list, Values@rules}] /. {x, i_} -> {i, i} /. {x^2, 
  i_} :> {i^2, i} /. {x^3, i_} :> {i^3, i})[[All, 1]]

and

  • $\#_2$

equivalently

(Thread[{list, Values@rules}] /. {x, i_} -> {i, i} /. {x^2, 
  i_} :> {i^2, i} /. {x^3, i_} :> {i^3, i})[[All, 1]]


  1. Laborious stuff, but works
  • $\#_1$

The following:

(Transpose[{list, Values@rules}] /. {x, i_} -> {i, i} /. {x^2, 
  i_} :> {i^2, i} /. {x^3, i_} :> {i^3, i})[[All, 1]]

and

  • $\#_2$

equivalently

(Thread[{list, Values@rules}] /. {x, i_} -> {i, i} /. {x^2, 
  i_} :> {i^2, i} /. {x^3, i_} :> {i^3, i})[[All, 1]]


I presented an additional way based on Michael E2's linked answer that needed a minor comment in this example, i.e how many times the rule has to be applied.
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
  1. While? also works works
Needs@"GeneralUtilities`"
Module[{hold},
  SetAttributes[hold, HoldAll];
  
  oneTimeRules[rules_] :=
   
   Normal@Merge[rules, ListIterator] /. Rule -> RuleDelayed /. 
     i_GeneralUtilities`Iterator :> 
      With[{r = Read[i]}, hold[r, r =!= IteratorExhausted]] /. 
    hold -> Condition;
  
  ];
 

and then

Replace[list, oneTimeRules@rules, Length@rules]
  1. Another fancy approach based on Michael E2's answer. This serves as a clarifying comment. Grab the relevant code that is needed
SetAttributes[useRepeated, Listable];
useRepeated[(Rule | RuleDelayed)[pat_, repl_], n_ : 1] :=
  Module[{used = 0},
   pat :> repl /; used++ < n
   ];
useOnce[r_] := useRepeated[r];

and then use

Replace[list, useOnce@rules, Length@rules]

or equivalently

ReplaceAll[list, useOnce@rules]

Note: if one uses Replace and the default value of the command -which is set to 1- the above does not work. So some minor caution is needed.

  1. While? also works works
Needs@"GeneralUtilities`"
Module[{hold},
  SetAttributes[hold, HoldAll];
  
  oneTimeRules[rules_] :=
   
   Normal@Merge[rules, ListIterator] /. Rule -> RuleDelayed /. 
     i_GeneralUtilities`Iterator :> 
      With[{r = Read[i]}, hold[r, r =!= IteratorExhausted]] /. 
    hold -> Condition;
  
  ];
 
Replace[list, oneTimeRules@rules, Length@rules]
  1. While also works
Needs@"GeneralUtilities`"
Module[{hold},
  SetAttributes[hold, HoldAll];
  
  oneTimeRules[rules_] :=
   
   Normal@Merge[rules, ListIterator] /. Rule -> RuleDelayed /. 
     i_GeneralUtilities`Iterator :> 
      With[{r = Read[i]}, hold[r, r =!= IteratorExhausted]] /. 
    hold -> Condition;
  
  ];

and then

Replace[list, oneTimeRules@rules, Length@rules]
  1. Another fancy approach based on Michael E2's answer. This serves as a clarifying comment. Grab the relevant code that is needed
SetAttributes[useRepeated, Listable];
useRepeated[(Rule | RuleDelayed)[pat_, repl_], n_ : 1] :=
  Module[{used = 0},
   pat :> repl /; used++ < n
   ];
useOnce[r_] := useRepeated[r];

and then use

Replace[list, useOnce@rules, Length@rules]

or equivalently

ReplaceAll[list, useOnce@rules]

Note: if one uses Replace and the default value of the command -which is set to 1- the above does not work. So some minor caution is needed.

added another way based on a previous answer by Michael E2
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
deleted 33 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
minor formatting of the code lines
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added another approach labelled numbered 0
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added 818 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added 33 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added a 9th way of obtaining the desired result
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
another solution based on For
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added 192 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added another way of getting the result
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added 55 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
added 55 characters in body
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading
Source Link
bmf
  • 17k
  • 2
  • 32
  • 68
Loading