Skip to main content
edited body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_](a[_] -> x_] x_):> x, Infinity]
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity]
Cases[solution, (a[_]HoldPattern[a[_] -> x_)x_] :> x, Infinity]

Remarks about Cases:

  • For your example, default levelspec for Cases, which is {1}, will do the job.
  • ReplaceAll (/.) will work with solution containing Rule (->) or RuleDelayed (:>) but for Cases we have to point this out:

solution = {a[1] -> 1, a[2] :> 5, a[3] -> 100}

Cases[solution, Rule[a[_], x_] :> x]
Cases[solution, (Rule | RuleDelayed)[a[_], x_] :> x]
{a[1] -> 1, a[2] :> 5, a[3] -> 100}
{1, 100}
{1, 5, 100}

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity]
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity]
Cases[solution, (a[_] -> x_):> x, Infinity]

Remarks about Cases:

  • For your example, default levelspec for Cases, which is {1}, will do the job.
  • ReplaceAll (/.) will work with solution containing Rule (->) or RuleDelayed (:>) but for Cases we have to point this out:

solution = {a[1] -> 1, a[2] :> 5, a[3] -> 100}

Cases[solution, Rule[a[_], x_] :> x]
Cases[solution, (Rule | RuleDelayed)[a[_], x_] :> x]
{a[1] -> 1, a[2] :> 5, a[3] -> 100}
{1, 100}
{1, 5, 100}

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, (a[_] -> x_):> x, Infinity]
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity]
Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity]

Remarks about Cases:

  • For your example, default levelspec for Cases, which is {1}, will do the job.
  • ReplaceAll (/.) will work with solution containing Rule (->) or RuleDelayed (:>) but for Cases we have to point this out:

solution = {a[1] -> 1, a[2] :> 5, a[3] -> 100}

Cases[solution, Rule[a[_], x_] :> x]
Cases[solution, (Rule | RuleDelayed)[a[_], x_] :> x]
{a[1] -> 1, a[2] :> 5, a[3] -> 100}
{1, 100}
{1, 5, 100}
added 495 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

This is not what RulesRules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper""proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity] (*here levelspec 1 will work too*)
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity]
Cases[solution, (*alsoa[_] for-> yourx_):> casex, levelspecInfinity]

Remarks about Cases:

  • For your example, default levelspec for Cases, which is {1}, will do the job.
  • ReplaceAll (/.) will work with solution containing Rule (->) or RuleDelayed (:>) but for Cases we have to point this out:

solution = {a[1] -> 1, willa[2] do*:> 5, a[3] -> 100}

Cases[solution, Rule[a[_], x_] :> x]
Cases[solution, (Rule | RuleDelayed)[a[_], x_] :> x]
{a[1] -> 1, a[2] :> 5, a[3] -> 100}
{1, 100}
{1, 5, 100}

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity] (*here levelspec 1 will work too*)
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity] (*also for your case levelspec 1 will do*)

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity]
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity]
Cases[solution, (a[_] -> x_):> x, Infinity]

Remarks about Cases:

  • For your example, default levelspec for Cases, which is {1}, will do the job.
  • ReplaceAll (/.) will work with solution containing Rule (->) or RuleDelayed (:>) but for Cases we have to point this out:

solution = {a[1] -> 1, a[2] :> 5, a[3] -> 100}

Cases[solution, Rule[a[_], x_] :> x]
Cases[solution, (Rule | RuleDelayed)[a[_], x_] :> x]
{a[1] -> 1, a[2] :> 5, a[3] -> 100}
{1, 100}
{1, 5, 100}
typo
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}
 

More "proper" way:

a /@ Range[3] /. solution
{1, 10, 100}

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on thatwhat is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity] (*here levelspec 1 will work too*)
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity] (*also for your case levelspec 1 will do*)
{1, 10, 100}

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}

More "proper" way:

a /@ Range[3] /. solution
{1, 10, 100}

For more nested list of rules you can use Cases (but then you have to keep an eye on that is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity] (*here levelspec 1 will work too*)
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity] (*also for your case levelspec 1 will do*)
{1, 10, 100}

This is not what Rules are made for but the following works well for simple cases:

solution[[All, 2]]
{1, 10, 100}
 

More "proper" way:

a /@ Range[3] /. solution

or equivalent suggested by Mr. Wizard

Array[a, 3] /. solution

For more nested list of rules you can use Cases (but then you have to keep an eye on what is what ;)) :

Cases[solution, HoldPattern[a[_] -> x_] :> x, Infinity] (*here levelspec 1 will work too*)
{1, 10, 100}

Or the same but in different form:

Cases[solution, Rule[a[_], x_] :> x, Infinity] (*also for your case levelspec 1 will do*)
added 1 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading