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$
8
$\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$
2
$\begingroup$
test = k == 2.7536 || k == 4.6917 || k == 6.3913 || k == 8.3237
Cases[test, _ == x_Real -> x]
$\endgroup$
  • $\begingroup$ Cases[ test, x_Real, 2] also work $\endgroup$ – Alucard Oct 3 '17 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$
  • $\begingroup$ Thanks that solved it! $\endgroup$ – Turbotanten Oct 3 '17 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, privacy policy and cookie policy

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