2
$\begingroup$

I have the following list:

 m = {{x == 0, y == 0.29264681456942615}, 
      {x == 30, y == 0.2419119568894183}, 
      {x == 50, y == 0.1485164898707659}, 
      {x == 70, y == 0.05437093382683481}, 
      {x == 90 , y == 1.}}

I would like to convert it to the form {{0,0.29264681456942615},{30, 0.2419119568894183}, ... } (extracting only the numbers form the list). How can I do this?

$\endgroup$
7
  • $\begingroup$ m[[;; , ;; , 2]] should do the trick. To see why check the result of the following: Head[a==b], (a==b)[[0]], (a==b)[[1]], (a==b)[[2]] $\endgroup$
    – ssch
    Nov 6 '12 at 14:32
  • 4
    $\begingroup$ alternatively m /. Equal[_, b_] -> b $\endgroup$
    – chris
    Nov 6 '12 at 14:42
  • $\begingroup$ That's way neater since it can be used even if the order was {3==x,8==y} in some places, m /. Equal[a_, b_] :> If[NumberQ[a], a, b] $\endgroup$
    – ssch
    Nov 6 '12 at 14:54
  • 2
    $\begingroup$ Map[Last, m, {2}]. $\endgroup$
    – J. M.'s torpor
    Nov 6 '12 at 14:56
  • 2
    $\begingroup$ @ssch equivalent but perhaps less cryptic for the novice: m[[All,All,2]] $\endgroup$
    – Yves Klett
    Nov 6 '12 at 15:05
11
$\begingroup$

One of many possible ways is

{x, y} /. (m /. Equal -> Rule)

What happens here: Let's say you want to transform the expression x equals 0 into x is replaced by 0, then you can do exactly this by ReplaceAll (ok, replacing all) Equals in your list into Rules which is done by (m /. Equal -> Rule). The rest is to use it and replace {x,y} with these rules.

One disadvantage of this approach which is shared by following solutions from the comments:

m[[;; , ;; , 2]]
m[[All,All,2]]
m[[All, All, -1]]
m /. Equal[_, b_] -> b
Map[Last, m, {2}]

is that it relies on the fact, that your number is in the end. This can be prevented by using for instance a rule which checks where the numeric value is like suggested by ssch

m /. Equal[a_, b_] :> If[NumberQ[a], a, b]
$\endgroup$
5
  • $\begingroup$ Hmmm... why the RuleDelayed in your first example? Rule works too. +1 for the didactic explanation. $\endgroup$ Nov 6 '12 at 15:18
  • $\begingroup$ @IstvánZachar Thx. No special reason. I use RuleDelayed more often, since in most of my applications I don't want to rhs to be evaluated. But I see, it is maybe confusing the OP here. $\endgroup$
    – halirutan
    Nov 6 '12 at 15:20
  • $\begingroup$ @halirutan the last rule still needs to be delayed (I couldn't edit because 1 character was considered a low quality edit by the system) $\endgroup$
    – ssch
    Nov 6 '12 at 15:31
  • $\begingroup$ @ssch Ah, yes, sorry. I was a bit beside me. $\endgroup$
    – halirutan
    Nov 6 '12 at 15:37
  • $\begingroup$ @ssch To explain why I thought it was not necessary: something like If[boing, a, b] stays unevaluated as long as boing is not True or False. I thought we can use this here and forgot that NumberQ[a] is instantly evaluated to False. Sorry again. $\endgroup$
    – halirutan
    Nov 6 '12 at 15:40
2
$\begingroup$
m = {{x == 0, y == 0.29264681456942615}, {x == 30, 
y == 0.2419119568894183}, {x == 50, 
y == 0.1485164898707659}, {x == 70, 
y == 0.05437093382683481}, {x == 90, y == 1.}};

m /. {x == a_, y == b_} -> {a, b}

This returns

{{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 1.}}
$\endgroup$
1
$\begingroup$

m = {{x == 0, y == 0.29264681456942615}, {x == 30, y == 0.2419119568894183}, {x == 50, y == 0.1485164898707659}, {x == 70, y == 0.05437093382683481}, {x == 90, y == 1.}};

If lists had this shape I would just

m[[All, All, 2]]

Alternatives are

m /. _ == n_ :> n

or

{x, y} /. (m /. Equal -> Rule)

or even

Block[{Equal = CompoundExpression}, m]

or odd

{x, y} /. First /@ Solve /@ m

all to get

(* {{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 
  1.}} *)
$\endgroup$
1
  • $\begingroup$ Ok, I just saw that all these except the even and odd ones were already mentioned in comments or answers $\endgroup$
    – Rojo
    Nov 7 '12 at 0:08
0
$\begingroup$
Block[{Equal = (#2 &)}, m]

or

m /. Equal -> (#2 &)

{{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 1.}}

$\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.