2
$\begingroup$

I have the following expression

K == 2.7536 || K == 4.6917 || K == 6.3913 || K == 8.3237

That I would like to convert into a table or list of numbers that look like the following.

{2.7536, 4.6917, 6.3913, 8.3237}

Is it possible and how can I do it?

$\endgroup$

4 Answers 4

9
$\begingroup$
expr = K == 2.7536 || K == 4.6917 || K == 6.3913 || K == 8.3237

K /. {ToRules[expr]}

Be aware that K is a built-in symbol and avoid assigning values to it (otherwise obscure things will get broken).

$\endgroup$
0
2
$\begingroup$
test = k == 2.7536 || k == 4.6917 || k == 6.3913 || k == 8.3237
Cases[test, _ == x_Real -> x]
$\endgroup$
1
  • $\begingroup$ Cases[ test, x_Real, 2] also work $\endgroup$
    – Alucard
    Oct 3, 2017 at 14:16
1
$\begingroup$

How about the function

extractor[arg_] := arg[[All, 2]][[#]] & /@ Range@Length@arg

with usage

extractor[K == 2.7536 || K == 4.6917 || K == 6.3913 || K == 8.3237]
$\endgroup$
1
  • $\begingroup$ Thanks that solved it! $\endgroup$ Oct 3, 2017 at 13:25
1
$\begingroup$

Here's a variation that indexes into the list and then Applies List:

stuff = K == 2.7536 || K == 4.6917 || K == 6.3913 || K == 8.3237;
List @@ stuff[[All, 2]]

{2.7536, 4.6917, 6.3913, 8.3237}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.